提问人:Elvin Mammadov 提问时间:10/19/2021 更新时间:10/19/2021 访问量:62
onDraw 方法不更改画布内的位图位置
onDraw method does not change bitmap position inside canvas
问:
我想从 Activity 类更改 Canvas 中位图的位置。我从下面的解决方案仅将 x(左)的第一个值绘制为零,稍后不会更改 x 值。但是我的 Logcat 显示,每次我从 Activity 调用它时,x 值都会发生变化。我想知道我的错误在哪里?onDraw 方法不更改位图的位置。
自定义视图:
public class TheCustomView extends View {
Bitmap scaledRollerBottom;
Bitmap backgroundMask;
Bitmap rollerBottom;
Bitmap overlayBitmap;
float x;
public TheCustomView(Context context) {
super(context);
}
public TheCustomView(Context context, AttributeSet attr) {
super(context, attr);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
backgroundMask = BitmapFactory.decodeResource(getResources(), R.drawable.background_new, options);
rollerBottom = BitmapFactory.decodeResource(getResources(), R.drawable.roller_bottom, options);
Bitmap scaledBackgroundMask = Bitmap.createScaledBitmap(backgroundMask, 1000, 1000, true);
scaledRollerBottom = Bitmap.createScaledBitmap(rollerBottom, 120, 31, false);
// int bitmap1Width = scaledBackgroundMask.getWidth();
// int bitmap1Height = scaledBackgroundMask.getHeight();
// overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, scaledBackgroundMask.getConfig());
}
@Override
protected void onDraw(Canvas canvas) {
Log.v("X ", "is " + x);
canvas.drawBitmap(scaledRollerBottom, (float) x, (float) 915, new Paint());
super.onDraw(canvas);
}
}
从活动类调用
if (callback2 > 0 && callback2 <= 0.1) {
customView.x = 468;
customView.draw(canvas);
// customView.invalidate();
} else if (callback2 < 0) {
customView.x = 650;
customView.draw(canvas);
// customView.invalidate();
} else if (callback2 > 0.1) {
customView.x = 270;
customView.draw(canvas);
//customView.invalidate();
}
答: 暂无答案
评论
invalidate
onDraw