Android Development 官方文档 图片图像Drawable部分阅读记录

一、概览

当需要在应用程序中显示静态图像时,可以使用drawable类及其子类来绘制形状和图像。drawable是可被绘制的内容的抽象。它的各种子类可用于特定的图像场景,您可以扩展它们来定义您自己的drawable实现你想要的功能。

    构建drawable对象:

      1.构造方法 Drawable drawable = new ShapeDrawable();

      2.加载项目里的image资源

      3.加载XML资源

注:在构建过程中,可以使用AAPT工具通过无损图像压缩自动优化放置在res/drawable/目录中的图像资源。例如,不需要超过256种颜色的真彩色PNG可以通过调色板转换为8位PNG。这会导致图像质量相同,但需要的内存更少。因此,放置在此目录中的映像二进制文件可以在生成时更改。如果计划将图像作为位流读取以将其转换为位图,请将图像放在res/raw/文件夹中,aapt工具不会修改它们。

demo1:使用drawable资源来创建image给imageView使用:

LinearLayout linearLayout;

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Create a LinearLayout in which to add the ImageView
  linearLayout = new LinearLayout(this);

  // Instantiate an ImageView and define its properties
  ImageView i = new ImageView(this);
  i.setImageResource(R.drawable.my_image);

  // set the ImageView bounds to match the Drawable's dimensions
  i.setAdjustViewBounds(true);
  i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
      LayoutParams.WRAP_CONTENT));

  // Add the ImageView to the layout and set the layout as the content view
  linearLayout.addView(i);
  setContentView(linearLayout);
}

demo2:把图片资源作为drawable对象处理

Resources res = context.getResources();
Drawable myImage = ResourcesCompat.getDrawable(res, R.drawable.my_image, null);

官网地址,翻译好累啊,还是先自己看一遍吧

猜你喜欢

转载自blog.csdn.net/hpp_1225/article/details/88822735