Android Canvas专注于选定区域 [英] Android Canvas focus in selected area

查看:135
本文介绍了Android Canvas专注于选定区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试达到这种效果:

I'm trying to achieve this effect:

我的想法是我想用部分透明层覆盖ImageView,但不是全部.在某个区域(圆形或矩形)的某个地方应该是完全透明的.我尝试使用以下代码行:

The idea is that I want to cover an ImageView with a partial transparent layer, but not all of it. Somewhere there is an area (circle or rectangle), that should be completely transparent. I tried it using this lines of code:

Paint paint = new Paint();
paint.setColor(Color.parseColor("#80000000"));
canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);
paint.setColor(Color.TRANSPARENT);
canvas.drawCircle((int) x, (int) y, 50, paint);

但是它不起作用,因为在圆圈下面有一个矩形. 反正有实现我想要的目标吗?

but it didn't work since under the circle there is the rectangle. Is there anyway to achieve what I'm trying to?

推荐答案

对不起,您回答的太迟了. 您可以通过制作CustomImageView来实现此目的,并按照以下步骤进行操作.

Sorry for the late answer. You can achieve this my making a CustomImageView and do as follow.

public class CustomImageView extends ImageView {

private Paint paint;
private Paint transParentPaint;
private Canvas tempCanvas;
private Bitmap emptyBitmap;

public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public CustomImageView(Context context) {
    super(context);
    init();
}

public CustomImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    paint = new Paint();
    paint.setColor(Color.parseColor("#80000000"));  //Semi TransParent Paint

    transParentPaint = new Paint();
    transParentPaint.setColor(Color.TRANSPARENT);
    transParentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//This is necessary to make the portion transparent. Otherwise it will show Black.
}


@Override
protected void onDraw(Canvas canvas) {
    emptyBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    tempCanvas = new Canvas(emptyBitmap);
    emptyBitmap.eraseColor(Color.TRANSPARENT);
    // tempCanvas.drawColor(Color.parseColor("#80000000"));
    tempCanvas.drawRect(0, 0, getWidth(), getHeight(), paint);
    tempCanvas.drawCircle(getWidth() / 2, getHeight() / 2, 100, transParentPaint); // Set the circle at the middle of the Canvas.
    canvas.drawBitmap(emptyBitmap, 0, 0, null);
}

}

将此CustomImageView添加到您的布局文件

Add this CustomImageView to your layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
>

<transparent.example.com.partialtransparentview.CustomImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/your_image" />
</RelativeLayout>

它将按预期工作.

这篇关于Android Canvas专注于选定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆