我正在使用画布制作一个简单的绘图应用程序问题是当我为画布设置背景图像(黑板)时,它只占屏幕的约66%。见截图/链接: Canvas View
我希望我的画布视图占据整个屏幕,除了具有id parLayout的RelativeLayout占据的底部部分。
我的代码中出错了什么?这就是我所拥有的:
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static int wandToUse1;
public static int wandToUse2;
public static int wandToUse3;
private ViewGroup rootLayout;
private ViewGroup rlWand2;
private int _xDelta;
private int _yDelta;
private RelativeLayout pl;
private RelativeLayout wn2;
private RelativeLayout wn1;
ImageView w1;
ImageView w2;
boolean clicked1 = false;
boolean clicked2 = false;
RelativeLayout ll;
public static int X;
public static int Y;
CanvasView canvasView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.parLayout);
canvasView = new CanvasView(MainActivity.this);
rootLayout.addView(canvasView);
RelativeLayout.LayoutParams layoutParams1w2 = new RelativeLayout.LayoutParams(getScreenWidth(), getScreenHeight() / 2);
layoutParams1w2.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
canvasView.setLayoutParams(layoutParams1w2);
ll = (RelativeLayout) findViewById(R.id.parLayout);
w1 = (ImageView) findViewById(R.id.wand1);
w2 = (ImageView) findViewById(R.id.wand2);
w1.setImageResource(wandToUse1);
w2.setImageResource(wandToUse2);
pl = (RelativeLayout) findViewById(R.id.coordAct);
wn1 = (RelativeLayout) findViewById(R.id.rlw1);
wn2 = (RelativeLayout) findViewById(R.id.rlw2);
w1.setOnTouchListener(new ChoiceTouchListener());
w2.setOnTouchListener(new ChoiceTouchListener());
}
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
public class ChoiceTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent event) {
if (!clicked1) {
rootLayout = (ViewGroup) w1.getParent();
if (rootLayout != null) {
rootLayout.removeView(w1);
}
rlWand2 = (ViewGroup) w2.getParent();
if (rlWand2 != null) {
rlWand2.removeView(w2);
}
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(300, 300);
pl.addView(w1);
RelativeLayout.LayoutParams layoutParams1w2 = new RelativeLayout.LayoutParams(250, 250);
wn2.addView(w2);
layoutParams1w2.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
w1.setLayoutParams(layoutParams1);
w2.setLayoutParams(layoutParams1w2);
clicked1 = true;
clicked2 = false;
}
X = (int) event.getRawX();
Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
canvasView.dispatchTouchEvent(event);
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
canvasView.dispatchTouchEvent(event);
break;
}
rootLayout.invalidate();
return true;
}
}
CanvasView.java
public class CanvasView extends View{
private Bitmap sourceBitmap;
private Canvas sourceCanvas = new Canvas();
private Paint destPaint = new Paint();
public static Path destPath = new Path();
Bitmap rawBitmap;
public int width;
public int height;
public CanvasView(Context context) {
super(context);
init(context);
}
public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CanvasView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
width = metrics.widthPixels;
height = metrics.heightPixels;
//converting drawable resource file into bitmap
rawBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.blackboard);
rawBitmap = Bitmap.createScaledBitmap(rawBitmap, width, height, false);
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
//converting bitmap into mutable bitmap
sourceBitmap = Bitmap.createBitmap(rawBitmap.getWidth(), rawBitmap.getHeight(), Bitmap.Config.ARGB_8888);
sourceCanvas.setBitmap(sourceBitmap);
sourceCanvas.drawBitmap(rawBitmap, frameToDraw,whereToDraw, null);
destPaint.setAlpha(0);
destPaint.setAntiAlias(true);
destPaint.setStyle(Paint.Style.STROKE);
destPaint.setStrokeJoin(Paint.Join.ROUND);
destPaint.setStrokeCap(Paint.Cap.ROUND);
//change this value as per your need
destPaint.setStrokeWidth(50);
destPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
@Override
protected void onDraw(Canvas canvas)
{
sourceCanvas.drawPath(destPath, destPaint);
canvas.drawBitmap(sourceBitmap, 0, 0, null);
super.onDraw(canvas);
}
public void clearCanvas() {
destPath.reset();
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
destPath.moveTo(MainActivity.X, MainActivity.Y-360);
break;
case MotionEvent.ACTION_MOVE:
destPath.lineTo(MainActivity.X, MainActivity.Y-360);
break;
default:
return false;
}
invalidate();
return true;
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordAct"
tools:context="com.simplepaintapp.MainActivity">
<RelativeLayout
android:id="@+id/parLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:background="@color/colorAccent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#BA9DF7"
android:gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:id="@+id/parentLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="5">
<RelativeLayout
android:id="@+id/rlw1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="0dp"
android:layout_weight="1"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/wand1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:src="@mipmap/pen" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlw2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="0dp"
android:layout_weight="1"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/wand2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:src="@drawable/eraser" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
谢谢!
答案 0 :(得分:0)
我明白了!我昨天有点累,想不到。 只需在MainActivity.java
中修复此行RelativeLayout.LayoutParams layoutParams1w2 = new RelativeLayout.LayoutParams(getScreenWidth(), getScreenHeight() / 2);
要
RelativeLayout.LayoutParams layoutParams1w2 = new RelativeLayout.LayoutParams(getScreenWidth(), getScreenHeight());