ImageView 透明度改变 位图的获取

本文展示了一个Android应用示例,通过监听Button点击事件动态改变ImageView的透明度,并在ImageView被触摸时截取指定区域的位图显示在另一个ImageView上。涉及到的关键技术包括位图操作和触摸事件处理。

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

<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"
  
    tools:context=".MainActivity" >


    <Button
        android:id="@+id/buttonup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="增加透明度" />


 <Button
     android:id="@+id/down"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/buttonup"
     android:layout_marginTop="15dp"
     android:text="降低透明度" />


 <Button
     android:id="@+id/next"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/down"
     android:layout_marginTop="18dp"
     android:text="下一张图片" />


 <ImageView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:layout_alignTop="@+id/down"
     android:scaleType="fitEnd"
     android:src="@drawable/wdga" />


 <ImageView
     android:id="@+id/imageView2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_marginBottom="50dp"
     android:src="@drawable/tdsaf" />
    

</RelativeLayout>



代码:

package com.example.imageview;


import android.R.plurals;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
int []images={R.drawable.adgagdasgda,R.drawable.geaerg,R.drawable.qdagga,R.drawable.rdagd,
R.drawable.tdsaf,R.drawable.wdga};


Button buup,budown,bunext;
ImageView im1,im2;

int count=2;
int alpha=255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buup=(Button) findViewById(R.id.buttonup);
        budown=(Button) findViewById(R.id.down);
        bunext=(Button) findViewById(R.id.next);
        
        im1=(ImageView) findViewById(R.id.imageView1);
        im2=(ImageView) findViewById(R.id.imageView2);
        
        
        bunext.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
im1.setImageResource(images[++count%images.length]);
}
});
        
        
        View.OnClickListener listener=new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==buup){
alpha+=20;
}if(arg0==budown){
alpha-=20;
}if(alpha>=255){
alpha=255;
}if(alpha<=0){
alpha=0;
}

im1.setAlpha(alpha);

}
};
        
        budown.setOnClickListener(listener);
        buup.setOnClickListener(listener);
        im1.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDrawable=(BitmapDrawable) im1.getDrawable();
//获取图片中的位图
Bitmap bitmap=bitmapDrawable.getBitmap();


double scale=1.0*bitmap.getHeight()/im1.getHeight();


int x=(int) arg1.getX();
int y=(int) arg1.getY();

if(x+120>bitmap.getWidth()){
x=bitmap.getWidth()-120;
}if(y+120>bitmap.getHeight()){
y=bitmap.getHeight()-120;
}
im2.setImageBitmap(Bitmap.createBitmap(bitmap,x, y, 120,120));

im2.setAlpha(alpha);

return false;
}

});
        
        
        
    }




    @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;
    }
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值