Gif图像视图非常非常滞后

时间:2017-07-28 10:44:50

标签: android gif

我正在尝试显示一个gif,它在模拟器中效果很好,但在我的真实设备上,它非常迟钝。我有Samsung Galaxy A3 - 2017。我尝试了许多库,例如Glide libraryIon libraryfelipecsl.gifimageview library,但没有任何效果。我试图从URL显示gif,甚至从drawable,raw和assets文件夹中显示,但没有。有什么想法吗?

目前我的项目中有这个:

布局:

<com.felipecsl.gifimageview.library.GifImageView
    android:id="@+id/gifImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottomNavBar"/>

MainActivity:

public class MainActivity extends AppCompatActivity {

GifImageView gifImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gifImageView = (GifImageView) findViewById(R.id.gifImageView);

    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavBar);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.back:
                    Toast.makeText(MainActivity.this, "Previous", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.start:
                    //Toast.makeText(MainActivity.this, "Start Gif", Toast.LENGTH_SHORT).show();
                    new RetriveByteArray().execute("http://gifdanceparty.giphy.com/assets/dancers/smooch.gif");
                    gifImageView.startAnimation();
                    break;

                case R.id.stop:
                    //Toast.makeText(MainActivity.this, "Stop Gif", Toast.LENGTH_SHORT).show();
                    gifImageView.stopAnimation();
                    break;

                case R.id.share:
                    Toast.makeText(MainActivity.this, "Share", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.next:
                    Toast.makeText(MainActivity.this, "Next", Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }
    });
}

RetriveByteArray:

private class RetriveByteArray extends AsyncTask<String, Void, byte[]>
{

    @Override
    protected byte[] doInBackground(String... strings) {
        try {
            URL url = new URL(strings[0]);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            if (urlConnection.getResponseCode() == 200)
            {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                int nRead;
                byte[] data = new byte[10240];
                while ((nRead = in.read(data, 0, data.length)) != -1)
                {
                    buffer.write(data, 0, nRead);
                }
                buffer.flush();
                return buffer.toByteArray();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(byte[] bytes) {
        super.onPostExecute(bytes);
        gifImageView.setBytes(bytes);
    }
}

修改编辑

任何想知道我如何解决它的人(感谢TGMCians回答):

添加依赖项:

`compile 'com.facebook.fresco:fresco:1.4.0'
// For animated GIF support
compile 'com.facebook.fresco:animated-gif:1.4.0'`

布局:

 <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

MainActivity:

public class MainActivity extends AppCompatActivity {

SimpleDraweeView mSimpleDraweeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);

    mSimpleDraweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);

 DraweeController controller = Fresco.newDraweeControllerBuilder()
                            .setUri("https://media4.giphy.com/avatars/100soft/WahNEDdlGjRZ.gif")
                            .setAutoPlayAnimations(true)
                            .build();
                    mSimpleDraweeView.setController(controller);

1 个答案:

答案 0 :(得分:0)

任何想知道我是如何修复它的人(感谢 TGMCians 的回答)

添加依赖:

compile 'com.facebook.fresco:fresco:1.4.0'
// For animated GIF support
compile 'com.facebook.fresco:animated-gif:1.4.0'

布局:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

主要活动:

public class MainActivity extends AppCompatActivity {

SimpleDraweeView mSimpleDraweeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fresco.initialize(this);
    setContentView(R.layout.activity_main);

    mSimpleDraweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);

 DraweeController controller = Fresco.newDraweeControllerBuilder()
                            .setUri("https://media4.giphy.com/avatars/100soft/WahNEDdlGjRZ.gif")
                            .setAutoPlayAnimations(true)
                            .build();
                    mSimpleDraweeView.setController(controller);
相关问题