for loop in run()SurfaceView Runnable Android
嗨,我正在尝试做一个应用程序,根据用户输入的点绘制线。用户可以输入尽可能多的点,对象将根据它们的x和y值绘制这些线。我查阅了许多绘图教程,发现在Android中使用for loop in run()SurfaceView Runnable Android,android,draw,surfaceview,runnable,Android,Draw,Surfaceview,Runnable,嗨,我正在尝试做一个应用程序,根据用户输入的点绘制线。用户可以输入尽可能多的点,对象将根据它们的x和y值绘制这些线。我查阅了许多绘图教程,发现在Android中使用SurfaceView Runnable是绘制任何类型的绘图或动画的最佳方法。我遇到了一个问题,run()函数没有使用for循环绘制。有没有办法让循环在run()或其他地方工作?我的代码如下 public class draw extends SurfaceView implements Runnable { Thread thre
SurfaceView Runnable
是绘制任何类型的绘图或动画的最佳方法。我遇到了一个问题,run()
函数没有使用for循环绘制。有没有办法让循环在run()
或其他地方工作?我的代码如下
public class draw extends SurfaceView implements Runnable {
Thread thread = null;
updateDraw draw = null;
boolean canDraw = false;
Path path;
Bitmap bitmap;
SurfaceHolder surfaceHolder;
Context mContext;
Paint paint;
int bitmapX;
int bitmapY;
int viewWidth;
int viewHeight;
ArrayList<ArrayList<Double>> XY;
Paint blue_paintbrush_stroke
public draw(Context context, ArrayList<ArrayList<Double>> XY) {
super(context);
mContext = context;
surfaceHolder = getHolder();
paint = new Paint();
path = new Path();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
draw = new updateDraw(viewWidth, viewHeight);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inMutable = true;
bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bitmap, options);
setUpBitmap();
}
@Override
public void run() {
Canvas canvas;
prepPaintBrushes();
while (canDraw) {
//draw stuff
if (surfaceHolder.getSurface().isValid()) {
int x = draw.getX();
int y = draw.getY();
canvas = surfaceHolder.lockCanvas();
canvas.save();
canvas.drawBitmap(bitmap, bitmapX, bitmapY, paint);
canvas.drawPath(path, blue_paintbrush_stroke);
for(int i = 0; i < XY.size()-1; i++){
float aX = (XY.get(i).get(0), XY.get(i).get(1)).get(0) + bitmapX;
float aY = (XY.get(i).get(0), XY.get(i).get(1)).get(1) + bitmapY;
float bX = (XY.get(i+1).get(0), XY.get(i+1).get(0)).get(0) + bitmapX;
float bY = (XY.get(i+1).get(0), XY.get(i+1).get(1)).get(1) + bitmapY;
canvas.drawLine(aX, aY, bX, bY, blue_paintbrush_stroke);
}
path.rewind();
canvas.restore();
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
private void updateFrame(int newX, int newY) {
draw.update(newX, newY);
}
private void setUpBitmap() {
bitmapX = (int) Math.floor(
Math.random() * (viewWidth - backGround.getWidth()));
bitmapY = (int) Math.floor(
Math.random() * (viewHeight - backGround.getHeight()));
}
public void pause() {
canDraw = false;
while (true) {
try {
thread.join();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void resume() {
canDraw = true;
thread = new Thread(this);
thread.start();
}
private void prepPaintBrushes() {
blue_paintbrush_stroke = new Paint();
blue_paintbrush_stroke.setColor(Color.BLUE);
blue_paintbrush_stroke.setStyle(Paint.Style.STROKE);
blue_paintbrush_stroke.setStrokeWidth(10);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setUpBitmap();
// Set coordinates of map.
updateFrame((int) x, (int) y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
// Updated coordinates for map.
updateFrame((int) x, (int) y);
invalidate();
break;
default:
// Do nothing.
}
return true;
}
}
公共类draw扩展了SurfaceView实现可运行{
线程=空;
updateDraw draw=null;
布尔canDraw=false;
路径;
位图;
浮雕浮雕;
语境;
油漆;
int-bitmappx;
int位映射;
内视图宽度;
内景高度;
阵列列表XY;
涂蓝漆\u画笔\u笔划
公共绘图(上下文,ArrayList XY){
超级(上下文);
mContext=上下文;
surfaceHolder=getHolder();
油漆=新油漆();
路径=新路径();
}
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
super.onSizeChanged(w,h,oldw,oldh);
视宽=w;
视高=h;
draw=新更新的draw(视图宽度、视图高度);
BitmapFactory.Options=new-BitmapFactory.Options();
options.inScaled=true;
options.inMutable=true;
bitmap=BitmapFactory.decodeResource(mContext.getResources(),R.drawable.bitmap,选项);
设置位图();
}
@凌驾
公开募捐{
帆布;
预处理刷子();
while(canDraw){
//画画
if(surfaceHolder.getSurface().isValid()){
int x=draw.getX();
int y=draw.getY();
canvas=surfaceHolder.lockCanvas();
canvas.save();
drawBitmap(位图、位图x、位图y、绘画);
画布绘制路径(路径,蓝色画笔笔划);
对于(int i=0;i
最好重写onDraw方法和
请确保您正在从父活动调用resume()方法。 差不多
@Override
protected void onResume(){
super.onResume();
customDraw.resume();
}