android sdk---ImageView的切换

本文介绍如何使用按钮实现对图像视图的切换功能,底层图像在按钮触发时进行变化,而上层图像保持不变,适用于图像展示与交互应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:

点击置左和置右按钮,底层图像进行切换,上层图像保持不动

代码如下

private ImageView myImageView1;
 private ImageView myImageView2;
 private Button myButtonLeft;
 private Button myButtonRight;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        myImageView1 = (ImageView)findViewById(R.id.myImageView1);
        myImageView2 = (ImageView)findViewById(R.id.myImageView2);
        myButtonLeft = (Button)findViewById(R.id.button1);
        myButtonRight = (Button)findViewById(R.id.button2);

        myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));
        myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.oa));
       
        myButtonLeft.setOnClickListener(new Button.OnClickListener()
        {
       
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.left));
   }});
       
        myButtonRight.setOnClickListener(new Button.OnClickListener()
        {
       
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));
   }});

XML:

/*底层图像*/
    <ImageView
        android:id="@+id/myImageView1"
        android:layout_width="320px"
        android:layout_height="280px"
        android:layout_x="0px"
        android:layout_y="36px" />

/*上层图像*/
   <ImageView
        android:id="@+id/myImageView2"
        android:layout_width="104px"
        android:layout_height="157px"
        android:layout_x="101px"
        android:layout_y="119px"
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="66dp"
        android:text="Left" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="40dp"
        android:text="Right" />

ImageView的放置顺序和图层的顺序有关

重点:

ImageView设置图片

myImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));

eclipse 项目文件浏览界面消失了:

解决方式:WINDOW--->SHOW VIEW-->Package Exploer

在res/drawable中添加图片的方式:

将图片粘贴到项目res/drawable的文件夹下,重启一下eclipse就可以了(暂时是我采用的,应该有不用重启的办法)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值