18

アニメーション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);
}}

スマートフォンのスクリーンショット: 私のスマートフォンのスクリーンショット エミュレーターのスクリーンショット: 私のエミュレータのスクリーンショット

アプリがスマートフォンで実行されないのはなぜですか?

4

3 に答える 3

13

ハードウェア アクセラレーションがオンになっているデバイスでは、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>
于 2013-02-08T19:29:55.623 に答える
2

アニメーション GIF を読み込むのは難しいようですが、代わりにWebViewを使用することをお勧めします。

問題を解決するには、次のようにします。

  1. GIF を Android Assets にコピーします。
  2. webView.loadDataWithBaseURL("file:///android_asset/", "", "text/html", "utf-8", null); を呼び出します。

私はそれが役立つことを願っています!

于 2014-09-15T13:38:24.467 に答える