gpt4 book ai didi

android 使用 XML 布局中的 View 来绘制 Canvas

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:02 27 4
gpt4 key购买 nike

所以基本上我想使用 xml 布局,但我还想要一个可以执行图形的 Canvas 。我所做的是在我的 xml 布局中创建一个 View ,如下所示。然后在我的应用程序中,我让 View 绘制了 Canvas ,但它不起作用。我不确定我解决这个问题的方法是完全错误的还是什么。因此,请看一下我的代码,然后告诉我您是否看到了快速修复或是否有更好的方法。提前致谢,我真的很感激。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/bTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />



<View
android:id="@+id/vMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

这是xml布局

package sm.view.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class ViewActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */

View v;
Button b;
boolean isRun =true;
SurfaceHolder ourHolder;
Thread ourThread;
Canvas canvas;
boolean isTure = true;
TheSurface ourSurfaceView;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b= (Button) findViewById(R.id.bTest);
v = (View) findViewById(R.id.vMain);

canvas = new Canvas();
ourSurfaceView = new TheSurface(this);
ourSurfaceView.setOnTouchListener(this);
v.draw(canvas);
// v.setBackgroundColor(Color.BLUE);
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurfaceView.pause();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ourSurfaceView.resume();
}






public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
public class TheSurface extends SurfaceView implements Runnable{

public TheSurface(Context context) {
super(context);
ourHolder= getHolder();

}
public void resume(){
isRun= true;
ourThread = new Thread(this);
ourThread.start();
}
public void pause(){
isRun = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread= null;
}

public void run() {
// TODO Auto-generated method stub

Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);
while(isTure){
if(!ourHolder.getSurface().isValid())
continue;
//v.draw(canvas);

canvas = ourHolder.lockCanvas();
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), textPaint);
ourHolder.unlockCanvasAndPost(canvas);
v.draw(canvas);
}
}

}

最佳答案

从这里开始(这也需要您输入命名空间部分“yourProjectNamespace”):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button android:id="@+id/bTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<sm.view.test.TheSurface android:id="@+id/vMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

在您的 TheSurface 中

实现可覆盖的例程:

public TheSurface(Context C){
super(C);

// Other setup code you want here
}

public TheSurface(Context C, AttributeSet attribs){
super(C, attribs);

// Other setup code you want here
}

public TheSurface(Context C, AttributeSet attribs, int defStyle){
super(C, attribs, defStyle);

// Other setup code you want here
}

protected void onDraw(Canvas canvas){
super.onDraw(canvas);

Paint textPaint = new Paint();
textPaint.setColor(Color.WHITE);

canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), textPaint);

// Other drawing functions here!!!
}

这应该可以完成您的绘图!!!

同样在我的例子中,你不必将它实现为 SurfaceView,你可以将它实现为 View,它不需要实现 runnable!!!

关于android 使用 XML 布局中的 View 来绘制 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com