动画完成android后绘制文字

时间:2014-03-06 05:06:20

标签: android android-canvas android-animation animationdrawable bitmapdrawable

我很困惑在完成动画后绘制文字 在这里,我尝试在完成动画后得到可绘制的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 个答案:

答案 0 :(得分: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;
}