gpt4 book ai didi

java - 如何使用canvas设置画线延迟

转载 作者:行者123 更新时间:2023-12-02 03:40:11 27 4
gpt4 key购买 nike

我是 Android 平台新手,尝试为每个像素绘制带有延迟的线。但是,当执行代码时,它们不会延迟在完整循环执行后要显示的每个像素,我不知道为什么要这样做。我需要对每个像素进行一些延迟,我的意思是在实时 View 中逐像素绘制。可能请建议示例代码或示例。提前致谢。

activity

public class MainActivity extends Activity {

ImageView drawingImageView;
int x1 = 50;
int y1 = 100;
int y2 = 100;
int i;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);

// Line
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
for (i = 0; i < 250; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
//error;
}
canvas.drawLine(x1, y1, x1 + i, y2, paint);
}
i++;
}}

最佳答案

请仔细阅读此内容。希望这对您有用。 :)

公共(public)类 TestActivity 扩展 AppCompatActivity {

ImageView drawingImageView;
int x1 = 50;
int y1 = 100;
int y2 = 100;
int x2=50, mCount = 1;
private Handler mHandler;
private Runnable mRunnable;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);

final Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);

canvas.drawLine(x1, y1, x2, y2, paint);

mHandler = new Handler(getMainLooper());
mRunnable = new Runnable() {
@Override
public void run() {
int x3 = x2 + mCount;
canvas.drawLine(x2, y1, x3, y2, paint);
x2 = x3;
mCount++;
drawingImageView.invalidate();
if (mCount < 150)
mHandler.postDelayed(mRunnable, 50);
}
};

mHandler.postDelayed(mRunnable,50);


}

}

关于java - 如何使用canvas设置画线延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912019/

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