Android中bitmap圖片透明度的處理(以撕美女衣服爲例)

原理介紹:將兩種不一樣效果的圖片放在相同的位置,改變上面的圖片的透明度,就能實現了。android

佈局文件:canvas

<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>

MainActivityapp

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;
        }
    }
}
相關文章
相關標籤/搜索