Нарисуйте текст после завершения анимации Android
Я запутался, чтобы нарисовать текст после завершения анимации здесь я попытался получить возможность рисовать Drawable temp = animationDrawable.getCurrent();
после завершения анимации и отправки в метод setDrawble(Drawable temp)
здесь я пишу текст в последнем кадре, возвращаюсь в imagview и устанавливаю imagedrawable.
предложить другой способ также
вот что я сделал, я публикую свой код
ImageView imageview;
AnimationDrawable animationDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keyframe_animation);
imageview = (ImageView) findViewById(R.id.imageview);
// Create the AnimationDrawable in which we will store all frames of the
// animation
animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw1), 0);
// Run until we say stop
animationDrawable.setOneShot(false);
imageview.setImageDrawable(animationDrawable);
imageview.setOnTouchListener(this);
}
protected void setBack() {
// TODO Auto-generated method stub
int num = 18;
Random rand = new Random();
int ran = rand.nextInt(num) + 9;
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw2), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw3), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw4), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw5), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw6), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw7), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw8), 200);
animationDrawable.addFrame(
getResources().getDrawable(R.drawable.bingo_nw9), 200);
imageview.setImageDrawable(animationDrawable);
animationDrawable.start();
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Drawable temp = animationDrawable.getCurrent();
imageview.setImageDrawable(setDrawble(temp));
}
}, 4000);
return true;
}
return false;
}
@SuppressWarnings("deprecation")
public Drawable setDrawble(Drawable temp) {
Bitmap bm = Bitmap.createBitmap(temp.getIntrinsicWidth(),
temp.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setTextSize(80);// Text Color
paint.setStrokeWidth(12); // Text Size
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text
// Overlapping
// Pattern
// some more settings...
canvas.drawBitmap(bm, 0, 0, paint);
canvas.drawText("Testing...", 10, 10, paint);
temp.setBounds(100, 100, canvas.getWidth(), canvas.getHeight());
temp.draw(canvas);
return temp;
}
1 ответ
Решение
Замените ваш обработчик, как это
animationDrawable.start();
long totalDuration = 0;
for(int i = 0; i< animation.getNumberOfFrames();i++){
totalDuration += animation.getDuration(i);
}
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Drawable temp = animationDrawable.getCurrent();
imageview.setImageBitmap(setDrawble(temp));
}
}, totalDuration);
public Bitmap setDrawble(Drawable temp) {
Bitmap bm = Bitmap.createBitmap(temp.getIntrinsicWidth(), temp.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
temp.setBounds(0, 0, bm.getWidth(), bm.getHeight());
temp.draw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.CYAN);
paint.setTextSize((int)(temp.getIntrinsicHeight()*0.3));// Text Color
paint.setStrokeWidth(12); // Text Size
canvas.drawText("Testing...", bm.getWidth() / 2, bm.getHeight() / 2,
paint);//change x and y position where you want in the image
return bm;
}