好的,让我们创建一个自定义图像视图,它通过覆盖 onDraw 解决了这个问题 :)
AlphaImageView.java
package com.example.shareintent;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AlphaImageView extends ImageView {
public AlphaImageView(Context context) {
super(context);
}
public AlphaImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas){
canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 0x66, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
super.onDraw(canvas);
}
}
你的活动 xml
<com.example.shareintent.AlphaImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="52dp"
android:layout_marginTop="50dp"
android:src="@drawable/ic_launcher" />