簡體   English   中英

Android - 觸摸以擦除前景ImageView的部分以顯示背景視圖

[英]Android - Touch to erase portions of foreground ImageView to expose background View

所以我一直在為這一天做好准備。 假設我有一個自定義的ImageView,我想覆蓋在背景視圖上(都在RelativeLayout中),當觸摸時,它會擦除​​View的源位圖部分,就像MS Paint中的擦除工具一樣,暴露下面的View。 我已經檢查了幾乎所有的線程(比如這個 ),他們建議在Paint對象中使用PorterDuff SRC模式,以及創建Canvas out out out源位圖的ARGB_8888卷影副本以應用屏蔽。

此外,由於我必須通過網絡下載,所以我無法提前設置疊加源,因此ImageView的縮放類型可以為我完成縮放。

每當我覆蓋onDraw時,當我在IV的位圖上應用擦除時,它會顯示黑色背景而不是下面的視圖,即使我將背景設置為透明。 所以我在最后一條繩子上為了揭開背景視圖該怎么做。

這是我到目前為止所擁有的:

視圖構造函數:

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));  
    paint.setColor(Color.TRANSPARENT);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(STROKE_WIDTH);
    paint.setAntiAlias(true);

overrode setImageBitmap從我重新配置的源位圖設置我的畫布:

public void setImageBitmap(Bitmap bitmap){
    super.setImageBitmap(bitmap);
    Drawable bd = getDrawable();
    if(bd == null){
        return;
    }

    Bitmap fullSizeBitmap = ((BitmapDrawable) bd).getBitmap();
    overlay = fullSizeBitmap.copy(Config.ARGB_8888, true);
    c2 = new Canvas(overlay);
}

onDraw方法:

protected void onDraw(Canvas canvas) {
    /*
     * Override paint call by re-drawing the view's Bitmap first, then overlaying our path on top of it
     */
    Drawable bd = getDrawable();
    if(bd == null){
        return;
    }
    Bitmap fullSizeBitmap = ((BitmapDrawable) bd).getBitmap();
    if(fullSizeBitmap != null && c2 != null){
        canvas.drawColor(Color.TRANSPARENT);
        c2.drawBitmap(fullSizeBitmap, 0, 0, null);
        c2.drawPath(path, paint);
        canvas.drawBitmap(overlay, 0, 0, null);
    }
}

我知道這是一個非常古老的問題,但我在這里解決了類似的問題。 也許它可以幫助將來的某個人。

從API級別11開始,您必須為ImageView指定這段代碼,以便在后面而不是黑色區域顯示圖像。

if (Build.VERSION.SDK_INT >= 11) {
    imageview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

這有助於我成功顯示背面圖像

你可以嘗試這個解決方案,它將幫助你在觸摸Android中刪除圖像..或下載一個演示示例

public class MainActivity extends Activity {

    Bitmap bp;
    Canvas bitmapCanvas;
    DrawView drawImg;
    LinearLayout ln1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ln1 = (LinearLayout) findViewById(R.id.ln1);
        drawImg = new DrawView(this);
        ln1.addView(drawImg);

    }



    public class DrawView extends View implements View.OnTouchListener {

        private int x = 0;
        private int y = 0;

        Bitmap bitmap;
        Path circlePath;
        Paint circlePaint;

        private final Paint paint = new Paint();
        private final Paint eraserPaint = new Paint();


        public DrawView(Context context){
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            this.setOnTouchListener(this);

            // Set background
            this.setBackgroundColor(Color.CYAN);
            bp = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

            // Set bitmap
            bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
            bitmapCanvas = new Canvas();
            bitmapCanvas.setBitmap(bitmap);
            bitmapCanvas.drawColor(Color.TRANSPARENT);
            bitmapCanvas.drawBitmap(bp, 0, 0, null);

            circlePath = new Path();
            circlePaint = new Paint();
            circlePaint.setAntiAlias(true);
            circlePaint.setColor(Color.BLUE);
            circlePaint.setStyle(Paint.Style.STROKE);
            circlePaint.setStrokeJoin(Paint.Join.MITER);
            circlePaint.setStrokeWidth(4f);

            // Set eraser paint properties
            eraserPaint.setAlpha(0);
            eraserPaint.setStrokeJoin(Paint.Join.ROUND);
            eraserPaint.setStrokeCap(Paint.Cap.ROUND);
            eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            eraserPaint.setAntiAlias(true);

        }

        @Override
        public void onDraw(Canvas canvas) {

            canvas.drawBitmap(bitmap, 0, 0, paint);
            bitmapCanvas.drawCircle(x, y, 30, eraserPaint);

            canvas.drawPath(circlePath, circlePaint);
        }

        public boolean onTouch(View view, MotionEvent event) {
            x = (int) event.getX();
            y = (int) event.getY();

            bitmapCanvas.drawCircle(x, y, 30, eraserPaint);

            circlePath.reset();
            circlePath.addCircle(x, y, 30, Path.Direction.CW);

            int ac=event.getAction();
            switch(ac){
                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, String.valueOf(x), Toast.LENGTH_SHORT).show();
                    circlePath.reset();
                    break;
            }
            invalidate();
            return true;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM