android自定义 view组件重绘问题
我自定义了一个view组件当做背景,是动态的。但是当我将它放进主activity中,它就莫名其妙的不动了。代码如下packagecom.example.zhuyao;im...
我自定义了一个view组件当做背景,是动态的。但是当我将它放进主activity中,它就莫名其妙的不动了。代码如下
package com.example.zhuyao;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
public class BeiJing extends View
{
final int BACK = 1215;
private Bitmap back;
final int WIDTH = 1215;
final int HEIGHT = 717;
private int startX = BACK - WIDTH;
public BeiJing(Context context, AttributeSet attrs) {
super(context, attrs);
back = BitmapFactory.decodeResource(context.getResources()
, R.drawable.back_img);}
public BeiJing(Context context){
super(context);
back = BitmapFactory.decodeResource(context.getResources()
, R.drawable.back_img);
final Handler handler = new Handler();
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
onDraw(null);
invalidate();
}
});
}
}).start();
}
@SuppressLint("DrawAllocation")
public void onDraw(Canvas canvas)
{
Bitmap bitmap2 = Bitmap.createBitmap(back, startX, 0, 800 , 600);
canvas.drawBitmap(bitmap2, 0 , 0 , null);
}
} 展开
package com.example.zhuyao;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
public class BeiJing extends View
{
final int BACK = 1215;
private Bitmap back;
final int WIDTH = 1215;
final int HEIGHT = 717;
private int startX = BACK - WIDTH;
public BeiJing(Context context, AttributeSet attrs) {
super(context, attrs);
back = BitmapFactory.decodeResource(context.getResources()
, R.drawable.back_img);}
public BeiJing(Context context){
super(context);
back = BitmapFactory.decodeResource(context.getResources()
, R.drawable.back_img);
final Handler handler = new Handler();
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
onDraw(null);
invalidate();
}
});
}
}).start();
}
@SuppressLint("DrawAllocation")
public void onDraw(Canvas canvas)
{
Bitmap bitmap2 = Bitmap.createBitmap(back, startX, 0, 800 , 600);
canvas.drawBitmap(bitmap2, 0 , 0 , null);
}
} 展开
3个回答
展开全部
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
onDraw(null);
invalidate();
}
});
}
}).start();
新线程实现了runnable接口但是 里面的 第一个 run 方法只执行一次就退出了。应该加入循环不断更新。而hander.post 里的那个runnable 外层每循环一次就post一次 所以不用加入循环语句。
boolean flag=true;
new Thread(new runnable{
public void run(){
try{
while(flag){
Thread.sleep(1);
handler.post(new Runnable() {
public void run() {
onDraw(null);
invalidate();
}
});
}catch (InterruptedException e) {
e.printStackTrace();
flag=flase;
}
}
}).start();
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
onDraw(null);
invalidate();
}
});
}
}).start();
新线程实现了runnable接口但是 里面的 第一个 run 方法只执行一次就退出了。应该加入循环不断更新。而hander.post 里的那个runnable 外层每循环一次就post一次 所以不用加入循环语句。
boolean flag=true;
new Thread(new runnable{
public void run(){
try{
while(flag){
Thread.sleep(1);
handler.post(new Runnable() {
public void run() {
onDraw(null);
invalidate();
}
});
}catch (InterruptedException e) {
e.printStackTrace();
flag=flase;
}
}
}).start();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |