i am trying to display gif image i have used this code
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MYGIFView(this));
}
}
and the MYGIFView class:
public class MYGIFView extends View {
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public MYGIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.piggy);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}
It shows gif image from drawale folder in emulator and gif worrks but when i run this in real device it shows nothing... Whats the problem here??
Movie Object is not work properly on Device above(4.x) as hardware acceleration is enabled in these devices. So by making hardware acceleration disabled, you can make images work on both emulator and on real device.
You can disabled hardware acceleration by adding
android:hardwareAccelerated="false"
in your manifest file inder activity tag
2.you can also disabled hardware acceration programatically
for example: MYGIFView is your custom view where you are going to show your GIF images
By using this your GIF image will show on both emulator and real device :)