-
24-10-2019 - |
题
我试图将两张图片彼此放置。顶部是透明的。最重要的是,我正在尝试添加透明的画布,以便用户可以绘制,键入,无论需要做什么以及可见的底部两个图像。
因此,我可以看到两个图像出现或仅仅是画布。每当我尝试显示画布时,似乎都对[覆盖它们的两个图像的p。
这是我到目前为止所拥有的。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
..... some code to set up the paint
}
private Paint mPaint;
private MaskFilter mEmboss;
private MaskFilter mBlur;
public class MyView extends View {
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
// Creating a new relative layout add the definition again.
RelativeLayout relativeLayout = new RelativeLayout(TwoPicksOnEachOther.this);
// Setting the orientation to vertical
////relativeLayout.setOrientation(LinearLayout.VERTICAL);
// Creating Fish image
final ImageView iv = new ImageView(TwoPicksOnEachOther.this);
iv.setImageResource(R.drawable.fish2);
// relative layout parameters
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
//iv.setId(1);
relativeLayout.addView(iv,lp);
// Creating transparent image with boat.
final ImageView iv2 = new ImageView(TwoPicksOnEachOther.this);
iv2.setImageResource(R.drawable.ctdeasytwo);
//iv2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
relativeLayout.addView(iv2,lp2);
mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
//mBitmap = BitmapFactory.decodeResource(getResources(),
// R.drawable.ctdeasytwo);
mCanvas = new Canvas(mBitmap);
// mCanvas.drawBitmap(mBitmap, 10, 10, null);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
................... More code to manage touch events.
现在,我怀疑画布不是相对布局的一部分。这可能是问题吗?如果是的,我不能做relativelayout.addview(McAnvas,lp);
有什么建议么?
解决方案
抱歉,必须等待八个小时才能回答我自己的问题。
我能够解决这个问题。我创建了一个课程。
public class newClass extends View {
public newClass (Context context){ super(context); }
...
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT); }
那我要做的就是
final newClass myCanvas = new newClass (this);
relativeLayout.addView(myCanvas,lp2);
工作正常...
其他提示
我设法做了类似的事情(在透明的画布上绘制,背后有背景位图)通过执行以下操作(仅显示代码的一部分):
public class MyView extends View {
public MyView(Context c) {
super(c);
Bitmap tmpBitmap;
// Create the background bitmap and convert it to a drawable object
mBackBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight(), aMatrix, false);
mBackBitmapDrawable = new BitmapDrawable(mBackBitmap);
// Set the drawable object as the background
setBackgroundDrawable(mBackBitmapDrawable);
// Create an empty bitmap for the canvas
mBitmap = Bitmap.createBitmap(tmpBitmap.getWidth(), tmpBitmap.getHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
//Log.d("MyView", "onDraw");
// Use transparent background
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
}
希望能帮助到你。
你可以参考这个带有触摸事件的画布中的图像
不隶属于 StackOverflow