【Android学习】ImageDecoder

最近在学习Android,参考《疯狂Android讲义 第4版》

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.showtext);
        imageView = findViewById(R.id.show);

        ImageDecoder.Source source = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            source = ImageDecoder.createSource(getResources(),R.drawable.fat_po);
            ImageDecoder.OnHeaderDecodedListener listener =
                    new ImageDecoder.OnHeaderDecodedListener(){
                        @Override
                        public void onHeaderDecoded(@NonNull ImageDecoder decoder, @NonNull ImageDecoder.ImageInfo info, @NonNull ImageDecoder.Source source) {
                            textView.setText("Size: "+info.getSize().getWidth()+" "+
                                    info.getSize().getHeight());
                            decoder.setTargetSize(600,580);
                        }
                    };
            try {
                Drawable drawable = ImageDecoder.decodeDrawable(source,listener);
                imageView.setImageDrawable(drawable);
                if (drawable instanceof AnimatedImageDrawable)
                    ((AnimatedImageDrawable) drawable).start();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

ImageDecoder 是
一种用于转换编码图像类(如PNGJPEGWEBPGIF,或HEIF)转换成DrawableBitmap对象。从Android9开始支持。

Alt+Enter 是Android studio的快捷键,可以对代码的提示建议,进行快速修改。

效果如下图,可以显示GIF动图

【Android学习】ImageDecoder

 

【Android学习】ImageDecoder

上一篇:python sys.path.append()


下一篇:Android 9功能和API