动画完成后绘制文字



我很困惑地绘制动画完成后绘制文本在这里,我尝试了完成动画后获得能够绘制的Drawable temp = animationDrawable.getCurrent();,然后发送到方法setDrawble(Drawable temp)
在这里,我在最后帧上写文字,然后返回Imageview并设置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;
}

替换处理程序
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;
}

最新更新