【问题标题】:Show .gif with android.graphics.Movie使用 android.graphics.Movie 显示 .gif
【发布时间】:2013-01-07 02:12:04
【问题描述】:

我正在尝试,创建一个带有动画 GIF 的视图..

当我尝试在模拟器中运行以下代码时,一切正常。但是当我尝试在真正的智能手机上运行时,什么都没有发生..

我的看法:

public class GIFView extends View {

private Movie mMovie;
private long movieStart;

public GIFView(Context context) {
    super(context);
    initializeView();
}

public GIFView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView();
}

public GIFView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initializeView();
}

private void initializeView() {
    InputStream is = getContext().getResources().openRawResource(
            R.drawable.cookies2);
    mMovie = Movie.decodeStream(is);
}

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    long now = android.os.SystemClock.uptimeMillis();

    if (movieStart == 0) {
        movieStart = (int) now;
    }
    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight()
                - mMovie.height());
        this.invalidate();
    }
}}

我的活动:

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     GIFView gifView = new GIFView(this);
     setContentView(gifView);
}}

我的智能手机截图: 我的模拟器截图:

为什么我的应用无法在智能手机上运行?

【问题讨论】:

  • 也许您应该尝试通过布局加载动画,就像这里的这个人:android-ever.com/2012/06/android-animated-gif-example.html,以防有一些布局/视图信息通过?否则你的代码看起来几乎一样。
  • Thanx @Alex:您的问题帮助我们在 android 中制作 gif 图像。

标签: android animated-gif


【解决方案1】:

我认为 Movie 对象不能在打开硬件加速的设备上正常工作(在 Android 4.x 中默认情况下,对于支持它的设备,它是打开的。你的模拟器可能不会。)

尝试添加

 android:hardwareAccelerated="false"

AndroidManifest.xml 中 MainActivity 的活动定义

例子:

<activity
    android:name=".MainActivity"
    android:hardwareAccelerated="false"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

【讨论】:

  • 成功了。谢谢!你能解释一下那里到底发生了什么吗?
【解决方案2】:

不要关闭整个应用程序的硬件加速。那是残废。只需将其关闭即可查看:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

【讨论】:

    【解决方案3】:

    加载动画 GIF 似乎很棘手,但我建议使用 WebView 作为替代方案。

    请按照以下步骤解决问题:

    1. 在 Android 资源中复制您的 GIF。
    2. 调用 webView.loadDataWithBaseURL("file:///android_asset/", "", "text/html", "utf-8", null);

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      相关资源
      最近更新 更多