温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

实现Button控制ImageView显示下一张照片

发布时间:2020-08-10 12:35:16 来源:网络 阅读:2062 作者:吃素只吃肉 栏目:移动开发

话不多说,直接上码:xml文件,简单的线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="下一张"
        android:layout_gravity="right" />

    <ImageView
        android:id="@+id/p_w_picpath2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/cat"
        android:scaleType="fitCenter"
    />
</LinearLayout>

效果图实现Button控制ImageView显示下一张照片




package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by Administrator on 2015/6/6.
 */
public class Textp_w_picpathActivity extends Activity{
    //定义一个访问照片的数组
    int[] p_w_picpaths=new int[]{
            R.drawable.cat,
            R.drawable.dog,
            R.drawable.happen,
            R.drawable.hashiqi,
            R.drawable.what,
            R.drawable.name,
            };
//定义默认显示的照片
    int currentImg=2;
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏TITLE
        setContentView(R.layout.p_w_picpathview);
        final ImageView p_w_picpath2=(ImageView)findViewById(R.id.p_w_picpath2);
        final Button next=(Button)findViewById(R.id.button3);
              //定义查看下一张图片的监听器
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //控制Image View显示下一张照片
                p_w_picpath2.setImageResource(
                        p_w_picpaths[++currentImg % p_w_picpaths.length]
                );
            }
        });


}}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI