温馨提示×

温馨提示×

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

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

图像按钮ImageButton和图像ImageView

发布时间:2020-04-01 21:01:43 来源:网络 阅读:1089 作者:没有水勒鱼 栏目:移动开发

在AndroidApp应用中,图像是必不可少的。我们可以通过图像ImageView来展示。


一、设计界面

  1、首先把a.jpg、b.jpg、c.jpg、d.jpg、e.jpg、prov.png、next.png图片复制到res/drawable-hdpi文件夹内。

图像按钮ImageButton和图像ImageView

2、打开“res/layout/activity_main.xml”文件,生成ImageButton按钮。

  (1)从工具栏向activity拖出1个图像ImageView、2个图像按钮ImageButton。该控件来自Image&Media。

图像按钮ImageButton和图像ImageView



3、打开activity_main.xml文件。

  我们把自动生成的代码修改成如下代码,具体为:

   (1)ImageView的id修改为picture;

  (2)“上一幅”按钮ImageButton的id修改为prov;

  (3)设置android:padding="0dp",按钮灰色边框去掉。

    (4)“下一幅”按钮ImageButton的id修改为next;

  (5)设置android:padding="0dp",按钮灰色边框去掉。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <ImageButton
        android:id="@+id/prov"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="99dp"
        android:layout_marginLeft="28dp"
        android:src="@drawable/prov" 
        android:padding="0dp"/>


    <ImageButton
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/prov"
        android:layout_marginLeft="79dp"
        android:layout_toRightOf="@+id/prov"
        android:src="@drawable/next" 
        android:padding="0dp"/>


    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/prov"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="101dp"
        android:src="@drawable/a" />


</RelativeLayout>


二、单击事件 

  打开“src/com.genwoxue.ImageView/MainActivity.java”文件。

  然后输入以下代码:

图像按钮ImageButton和图像ImageView


在以上代码中,我们着重分析一下带有浅蓝色背景部分。

  1、第①部分

  导入与ImageView、ImageButton相关的包。

  2、第②部分

  声明ImageView、ImageButton控件变量。

  3、第③部分

  声明整型数组iImages用于存储图片资源。

  4、第④部分

  (1)findViewById()方法完成ImageView、ImageButton控件的捕获。

  (2)“上一幅”、“下一幅”按钮添加单击监听事件:ibtnProv.setOnClickListener(new ProvOnClickListener())、ibtnNext.setOnClickListener(new NextOnClickListener())。

  5、第⑤部分

  (1)我们新建一个类ProvOnClickListener继承接口OnClickListener用以实现单击事件监听。

  (2)单击按钮能够显示上一幅图片,如果到头了,则重置到最后一幅。

  6、第⑥部分

  (1)我们新建一个类NextOnClickListener继承接口OnClickListener用以实现单击事件监听。

  (2)单击按钮能够显示下一幅图片,如果到头了,则重置到第一幅。

直接写成匿名内部类也行:


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;


public class MainActivity extends Activity {
private ImageView ivwPicture = null;
private ImageButton ibtnProv = null;
private ImageButton ibtnNext = null;
private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivwPicture = (ImageView) super.findViewById(R.id.picture);
ibtnProv = (ImageButton) findViewById(R.id.prov);
ibtnNext = (ImageButton) findViewById(R.id.next);
ibtnProv.setOnClickListener(new OnClickListener(){
private int i = 5;
public void onClick(View v) {
if(i>0){
ivwPicture.setImageResource(iImages[--i]);
}else if(i==0){
i = 4;
ivwPicture.setImageResource(iImages[4]);
}
}
});
ibtnNext.setOnClickListener(new OnClickListener(){
private int i = 0;
public void onClick(View v) {
if(i<5){
ivwPicture.setImageResource(iImages[i++]);
}else if(i==5){
i = 1;
ivwPicture.setImageResource(iImages[0]);
}
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


向AI问一下细节

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

AI