Create a new Android project with empty activity target API 28. Currently, ImageDecoder have not imcluded in Support Library, so make sure to uncheck Backwards Compatibility. May be it will be included in someday.
Copy your GIF to /res/drawable / folder
Modify layout to include a ImageView
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/gifimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Example code to display animated GIF using ImageDecoder
package com.blogspot.android_er.androidimagedecoder;
import android.app.Activity;
import android.graphics.ImageDecoder;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.gifimage);
loadGif(imageView);
}
private void loadGif(ImageView iv){
try {
ImageDecoder.Source source =
ImageDecoder.createSource(getResources(), R.drawable.android_er);
Drawable drawable = ImageDecoder.decodeDrawable(source);
iv.setImageDrawable(drawable);
if (drawable instanceof AnimatedImageDrawable) {
((AnimatedImageDrawable) drawable).start();
Toast.makeText(getApplicationContext(),
"Animation started",
Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"IOException: \n" + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
When I test on Android Emulator, I have to disable hardwareAccelerated in AndroidManifest.xml, otherwise the GIF will not shown.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.android_er.androidimagedecoder">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:hardwareAccelerated="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This video show how it work on Android P Emulator.
Next:
- Apply PostProcess for ImageDecoder
- Mix using of ImageDecoder and ObjectAnimator
No comments:
Post a Comment