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

來源:互聯網
上載者:User

標籤:

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

布局檔案:

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

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.