我有点问题,我想将XML布局与视图类连接起来 哪个画画。主要想法是我想要绘制布局。
XML文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/image_areas"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/invert"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/a"
android:visibility="invisible"
/>
</FrameLayout>
Java代码
public class TouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public TouchEventView(Context ctx, AttributeSet attrs)
{
super(ctx,attrs);
paint.setAntiAlias(true);
paint.setColor(Color.YELLOW);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);}
protected void onDraw(Canvas canvas){
canvas.drawPath(path, paint);}
@Override
public boolean onTouchEvent(MotionEvent event){
int xPos = (int) event.getX();
int yPos = (int) event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(xPos, yPos);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPos, yPos);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;}
我尝试了很多东西,但没有任何效果。它一直显示一个空白的白色屏幕。
提前致谢!
答案 0 :(得分:0)
您必须创建custom View,然后将其包含在XML布局定义中。
答案 1 :(得分:0)
您可以尝试代码。例如
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_my_vault, null,false);
this.addContentView(viewToLoad);