实现一个图片浏览器,可以改变透明度,并提供触屏细节查看功能。

为了实现查看图片细节,需在xml布局文件里设置两个imageview,一个显示图片整体,一个显示局部细节

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/LinearLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="10dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

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

        <Button
            android:id="@+id/minus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="降低透明度" />

    <!-- 定义显示图片整体的ImageView -->
    <ImageView
        android:id="@+id/image1"
        android:layout_width="380px"
        android:layout_height="280px"
        android:src="@drawable/food"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="图片细节"/>

    <!-- 定义显示图片局部细节的imageView -->
    <ImageView
        android:id="@+id/image2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="#000000"
        android:layout_marginTop="10dp" />
</LinearLayout>
  1. 为了能动态改变图片透明度,需为按钮设置监听,再利用imageview的setAlpha()方法设置透明度
  2. 为了能根据触屏位置实时显示图片细节,需为第一个imageview设置setOnTouchListener,用户在第一个Imageview上发生触摸事件时,程序从原始图片中读取相应部分的图片,并将其显示在第二个ImageView中
  3. 用到了Bitmap类,它是一个代表位图的类,调用它的creatBitmap()静态方法即可截取位图的指定部分,返回截取区域生成的新位图。

    主程序代码:

public class MainActivity extends Activity{

    //定义图片的初始透明度
    private int alpha=255;
    private Button plus, minus;
    private ImageView image1, image2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        plus = (Button)findViewById(R.id.plus);
        minus = (Button)findViewById(R.id.minus);
        image1 = (ImageView)findViewById(R.id.image1);
        image2 = (ImageView)findViewById(R.id.image2);
        //定义改变图片透明度的办法
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v==plus) {
                    alpha+=20;
                }
                if (v==minus) {
                    alpha-=20;
                }
                if (alpha>=255) {
                    alpha=255;
                }
                if (alpha<=0) {
                    alpha=0;
                }
                //改变图片的透明度
                image1.setAlpha(alpha);
            }
        };
        //为两个按钮添加监听器
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
        image1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                BitmapDrawable bitmapDrawable=(BitmapDrawable)image1.getDrawable();
                //获取第一个图片显示框中的位图
                Bitmap bitmap=bitmapDrawable.getBitmap();
                //bitmap图片实际 大小与第一个imageView的缩放比例
                double scale=bitmap.getWidth()/320.0;
                //获取从需要显示的图片的开始点
                int x=(int)(event.getX()*scale);
                int y=(int)(event.getY()*scale);
                if (x+120>bitmap.getWidth()) {
                    x=bitmap.getWidth()-120;
                }
                if (x+120>bitmap.getWidth()) {
                    x=bitmap.getWidth()-120;
                }
                //显示图片的指定区域
                image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
                image2.setAlpha(alpha);
                return false;
            }
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值