原理介绍:将两种不同效果的图片放在相同的位置,改变上面的图片的透明度,就能实现了。

布局文件:

<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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:id="@+id/iv_bottom" />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:id="@+id/iv_top" /></RelativeLayout>

MainActivity

package cn.seanlou.stripclothes;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.ImageView;public class MainActivity extends Activity {    private ImageView ivBottom;    private ImageView ivTop;    private Bitmap imgBlank;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findView();        init();    }    /**     * 初始化控件内容     */    private void init() {        BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize = 1;        //获取显示在上面的图片,BitmapFactory.decodeResource()方法获取到的图片编码是RGB格式的,需要转换成ARGB格式的。        Bitmap imgTop = BitmapFactory.decodeResource(getResources(), R.mipmap.g1_up, options);        //获取显示在下面的图片。        Bitmap imgBottom = BitmapFactory.decodeResource(getResources(), R.mipmap.g1_back, options);        //创建一个显示在上面的图片imgTop大小的空白Bitmap图片,图片格式设置成ARGB格式的。        imgBlank = Bitmap.createBitmap(imgTop.getWidth(), imgTop.getHeight(), Bitmap.Config.ARGB_4444);        //将imgBlank创建为画布。        Canvas canvas = new Canvas(imgBlank);        //将imgTop画在画布上        canvas.drawBitmap(imgTop, 0, 0, null);        ivTop.setImageBitmap(imgBlank);        ivBottom.setImageBitmap(imgBottom);        ivTop.setOnTouchListener(new MyOnTouchListener());    }    private void findView() {        ivTop = (ImageView) findViewById(R.id.iv_top);        ivBottom = (ImageView) findViewById(R.id.iv_bottom);    }    private class MyOnTouchListener implements View.OnTouchListener {        @Override        public boolean onTouch(View v, MotionEvent event) {            if (event.getAction() == MotionEvent.ACTION_MOVE) {                int x = (int) event.getX();                int y = (int) event.getY();                Log.i("location", "当前位置 x:" + x + ",y:" + y);                for (int i = x - 20; i < x + 20; i++) {                    for (int j = y - 20; j < y + 20; j++) {                        //处理图片边界问题                        if (i >= 0 && i < imgBlank.getWidth() && j >= 0 && j < imgBlank.getHeight()) {                            //设置当前点为透明                            imgBlank.setPixel(i, j, Color.TRANSPARENT);                        }                    }                }                //显示图片                ivTop.setImageBitmap(imgBlank);            }            return true;        }    }}

更多相关文章

  1. 一个不错的启动菜单显示屏动画效果
  2. android textview属性
  3. Android(安卓)图片Bitmap的剪切
  4. 介绍两个Android开源项目:Android显示GIF动画
  5. android 自定义通知消息设置背景色不生效,导致部分机型显示白色字
  6. Java乔晓松-android中上传图片到服务器Tomcat(Struts2)
  7. Android图片下载缓存库picasso解析
  8. Android(安卓)调用相册 拍照 实现系统控件缩放 切割图片
  9. Android之selector标签

随机推荐

  1. Android(安卓)获取设备宽高分辨率
  2. android(6)(读数据的一些权限)
  3. android监控程序状态(安装 卸载)
  4. android 手电筒
  5. Android中获取和设置手机的壁纸
  6. Android(安卓)http文件上传-本地+服务器
  7. android 多个activity 共用一套广播时,写
  8. Android(安卓)优化
  9. Android获取手机的型号和系统版本
  10. Android.StructureOfAndroidSourceCodeRo