GIF 图像在模拟器上有效,但在真实设备上无效? [英] GIF image works on emulator but not in real device?

查看:36
本文介绍了GIF 图像在模拟器上有效,但在真实设备上无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示我使用过此代码的 gif 图像

i am trying to display gif image i have used this code

主活动

       public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MYGIFView(this));
       }

 }

MYGIFView 类:

 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();
}
}

它显示模拟器中 drawale 文件夹中的 gif 图像并且 gif 工作,但是当我在 真实设备中运行它时,它什么也没显示...这里有什么问题??

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

推荐答案

由于在这些设备中启用了硬件加速,因此影片对象在高于 (4.x) 的设备上无法正常工作.因此,通过禁用硬件加速,您可以使图像在模拟器和真实设备上都能正常工作.

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. 您可以通过添加

  1. You can disabled hardware acceleration by adding

android:hardwareAccelerated="false"

android:hardwareAccelerated="false"

在您的清单文件 inder 活动标签中

in your manifest file inder activity tag

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

2.您还可以通过编程方式禁用硬件加速

2.you can also disabled hardware acceration programatically

例如:MYGIFView 是您要在其中显示 GIF 图像的自定义视图

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);

通过使用它,您的 GIF 图像将同时显示在模拟器和真实设备上 :)

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

这篇关于GIF 图像在模拟器上有效,但在真实设备上无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆