![](/img/trans.png)
[英]How can I pass a Paint or Color to my new object that is using the onDraw() method from an activity?
[英]draw transparent color in onDraw using Paint Android
你好,我想在畫布和繪畫的幫助下在位圖上繪制顏色。 在位圖上進行顏色繪制工作正常,但我想在此處繪制不起作用的透明顏色,這是我的應用程序繪制黑色,如下所示
我從一個星期開始嘗試,但沒有成功,我想繪制透明的顏色,如下所示
我該怎么辦呢有誰在這里幫助我的是我的drawView類
public class DrawView extends View {
public Bitmap mBitmap;
public Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
private Bitmap imgF;
private static float MIN_ZOOM = 1f;
private static float MAX_ZOOM = 1f;
private float scaleFactor = 1.f;
private ScaleGestureDetector detector;
public DrawView(Context c, AttributeSet attrs) {
super(c, attrs);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setAlpha(200 * 5);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(9);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
imgF = Bitmap.createScaledBitmap(MainActivity.gImg, canvas.getWidth(),
canvas.getHeight(), true);
canvas.drawBitmap(imgF, 0, 0, mBitmapPaint);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
canvas.restore();
}
}
這是我的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/transparent" <---- transparent background
// below the FrameLayout
android:orientation="vertical" >
<FrameLayout
android:id="@+id/eraserFrameLayout"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" >
<com.dxd.bgeraser.DrawView
android:id="@+id/eraserImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<View style="@style/ShadowBarTop" />
<View style="@style/ShadowBarBottom" />
</FrameLayout>
</LinearLayout>
確保在保存圖像時壓縮格式為.png
如果
Bitmap.CompressFormat.JPEG
然后將其替換為
Bitmap.CompressFormat.PNG
透明的顏色代碼是:#00000000或可以使用Color.TRANSPARENT
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.