GIF image works on emulator but not in real device

2019-07-19 07:52发布

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??

1条回答
Explosion°爆炸
2楼-- · 2019-07-19 08:22

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.

  1. You can disabled hardware acceleration by adding

    android:hardwareAccelerated="false"

in your manifest file inder activity tag

<activity
    android:name=".MainActivity"
    android:hardwareAccelerated="false"
    android:label="@string/app_name" > 
</activity>

2.you can also disabled hardware acceration programatically

for example: MYGIFView is your custom view where you are going to show your GIF images

MYGIFView mMYGIFView =(MYGIFView)findViewById(R.id.myGifImageView);
mMYGIFView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

By using this your GIF image will show on both emulator and real device :)

查看更多
登录 后发表回答