Drawing translucent bitmaps using Canvas (Android)

android, java

Solution

You should be able to define a tween animation and apply that animation to an imageView. The XML resource would be similar to the following (named fade_animation.xml):

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator"
   android:fromAlpha="1.0"
   android:toAlpha="0.0"
   android:duration="100" />

then you would load and apply this animation to your custom imageView when ready:

ImageView sprite = (ImageView) findViewById(R.id.sprite);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_animation);
sprite.startAnimation(animation);

There are other options too. If you are using bitmaps and you don't want to do an animation, then you can manually decrease the alpha value of each frame, somewhat like this:

paint.setAlpha(100);
canvas.drawBitmap(spriteImage, left, top, paint);

Did you try any of these options?

Problem

I have a Bitmap object and want to render it to a Canvas object with varying levels of translucency (i.e. make the whole bitmap partially see through). For example, I have sprites in a game (that are drawn over the top of a bitmap background) that I want to fade out from being opaque to being invisible. Can I do this without having to resort to OpenGL?

Original source