public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取TextView对象
TextView textView = findViewById(R.id.tv);
// 获取ImageView对象
ImageView imageView = findViewById(R.id.image);
// ①、创建ImageDecoder.Source对象
ImageDecoder.Source source = ImageDecoder.createSource(getResources(), R.drawable.fat_po);
try {
// ②,执行decodeDrawable()方法获取Drawable对象
@SuppressLint("WrongThread") Drawable drawable = ImageDecoder.decodeDrawable(source, (decoder, info, s) -> {
// 通过info参数获取被解码的图片信息
textView.setText("图片原始宽度" + info.getSize().getWidth()
+ "\n" + "图片原始高度" + info.getSize().getHeight());
// 设置图片解码之后的缩放大小
decoder.setTargetSize(600, 580);
});
imageView.setImageDrawable(drawable);
// 如果drawable是AnimatedImageDrawable的实例,执行动画
if (drawable instanceof AnimatedImageDrawable) {
((AnimatedImageDrawable) drawable).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
摘抄至《疯狂Android讲义(第4版)》
网友评论