Android笔记(二十八) Android中图片之简单图片使用
用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能。
简单使用图片
使用Drawable对象
为Android应用增加了Drawable资源之后,系统会自动在R.java文件中创建一个索引项:R.drawable.fileName,然后在Java中可以通过R.drawable.fileName来获取到该资源的索引(一个int类型的常量),如果要获取实际的Drawable对象,则可以调用Resources的getDrawable(int id)方法来获取。
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageResource(R.drawable.pic1);
Bitmap和BitmapFactory
Bitmap代表一张位图,BitmapDrawable里封装的图片是一个Bitmap对象。开发者为了把一个Bitmap对象包装成一个BitmapDrawable对象,可以调用BitmapDrawable的构造器:
BitmapDrawable bd = new BitmapDrawable(bitmap);
如果需要获取BitmapDrawable所包装的Bitmap对象,则可调用BitmapDrawable的getBitmap()方法
Bitmap bitmap = drawable.getBitmap();
BitmapFactory是一个工具类,用于从不同的数据源来解析、创建Bitmap对象
BitmapFactory提供了一系列方法来帮助我们创建一个Bitmap对象,然后我们可以通过
imageView.setImageBitmap(Bitmap bm)
来更改一个ImageView显示的图像。
由于系统内容比较小,如果系统不停的去解析、创建Bitmap对象,可能会有内存溢出错误,所以Android为Bitmap提供了两个方法来判断它是否已经回收,如果没有,则强制Bitmap回收自己
boolean isRecycled(); 判断该Bitmap对象是否已被回收
void recycle() 强制Bitmap对象回收自己
一个例子:
package cn.lixyz.bitmaptest; import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; public class MainActivity extends Activity implements View.OnClickListener { private ImageView imageView;
private ArrayList<String> images;
private Button btnNext;
private Button btnLast;
private int index = 0;
private AssetManager am; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获得组件
imageView = (ImageView) findViewById(R.id.image);
btnNext = (Button) findViewById(R.id.next);
btnLast = (Button) findViewById(R.id.last); //调用getImages方法,获取assets下的图片集合
getImages(); //点击按钮
btnNext.setOnClickListener(this);
btnLast.setOnClickListener(this);
} /**
* 因为assets下不光有图片,还会有其他的目录或文件,需要将图片甄别出来存到一个list中当作数据源
*/
public void getImages() {
String[] tmpImgs = null;
images = new ArrayList<String>();
//getAssets()方法可以获得AssetManager对象
am = getAssets();
try {
//获取asset下内容list
tmpImgs = am.list("");
//挑出.jpg文件,存入list中
for (int i = 0; i < tmpImgs.length; i++) {
if (tmpImgs[i].endsWith(".jpg")) {
images.add(tmpImgs[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 点击按钮事件
*
* @param v view对象,用于判断点击的是什么按钮
*/
@Override
public void onClick(View v) { switch (v.getId()) {
case R.id.next:
try {
index++; //下标+1,用于显示下一张图片
if (index >= images.size()) { //防止越界
index = 0;
} //判断Bitmap是否已经回收,如果没有回收,则先回收
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
if (bd != null && !bd.getBitmap().isRecycled()) {
bd.getBitmap().recycle();
} //AssetManager类的open方法,可以返回一个输入流
InputStream is = am.open(images.get(index));
//通过BitmapFactory的decodeStream()方法,改变显示图像
imageView.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.last:
try {
index--; //下标+1,用于显示下一张图片
if (index < 0) { //防止越界
index = images.size() - 1;
}
//判断Bitmap是否已经回收,如果没有回收,则先回收
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
if (bd != null && !bd.getBitmap().isRecycled()) {
bd.getBitmap().recycle();
}
//AssetManager类的open方法,可以返回一个输入流
InputStream is = am.open(images.get(index));
//通过BitmapFactory的decodeStream()方法,改变显示图像
imageView.setImageBitmap(BitmapFactory.decodeStream(is));
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
MainActivity.java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"> <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout> <LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"> <Button
android:id="@+id/last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="上一张" /> <Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="下一张" />
</LinearLayout>
</RelativeLayout>
activity_main.xml
运行结果:


Android笔记(二十八) Android中图片之简单图片使用的更多相关文章
- Android笔记二十四.Android基于回调的事件处理机制
假设说事件监听机制是一种托付式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器全然消失了,当用户在GUI控件上激发某个事件时,控 ...
- Java学习笔记二十八:Java中的接口
Java中的接口 一:Java的接口: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承 ...
- Android进阶(二十八)上下文菜单ContextMenu使用案例
上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...
- Android笔记(七十五) Android中的图片压缩
这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...
- Android笔记(六十六) android中的动画——XML文件定义属性动画
除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...
- Android笔记(六十五) android中的动画——属性动画(propertyanimation)
补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...
- Android笔记(六十八) Fragment总结
Fragment的产生: 为了适应各种尺寸的屏幕,谷歌推出Fragment,可以把Fragment成Activity的一个组成部分,它拥有自己的生命周期.可以接收并处理用户的各种事件,还可以动态的增删 ...
- Android笔记(十八) 下拉列表(Spinner)
App中常用的控件——下拉列表(Spinner),提供特定选择供用户选择 Spinner每次只能选择一个部件,它的选项来自于与之相关联的适配器(apater)中. MainActivity.java ...
- Android笔记(十) Android中的布局——表格布局
TableLayout运行我们使用表格的方式来排列控件,它的本质依然是线性布局.表格布局采用行.列的形式来管理控件,TableLayout并不需要明确的声明包含多少行多少列,而是通过添加TableRo ...
- Android笔记(六十四) android中的动画——补间动画(tweened animation)
补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...
随机推荐
- Linux系统swappiness参数在内存与交换分区之间优化作用
http://blog.sina.com.cn/s/blog_13cc013b50102wskd.html swappiness的值的大小对如何使用swap分区是有着很大的联系的.swappiness ...
- FormsAuthentication使用指南
配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...
- iOS 多线程的简单理解(2) 队列 :串行 ,并行,MainQueue,GlobalQueue
多线程队列是装载线程任务的队形结构.(系统以先进先出的方式调度队列中的任务执行 FIFO).在GCD中有两种队列: 串行队列.并发队列. 队列 :串行队列.并发队列,全局主对列,全局并发队列 2.1. ...
- Appium移动自动化测试-----(五) java-client安装与测试
前提条件 当你点击这一章时,说明你是打算使用 Java 语言编写 appium 自动化测试脚本的. 1.安装 Java 环境 ,我想这一步你已经搞定了 2.安装 IntelliJ IDEA , 当然, ...
- git config命令详解
Git有一个工具被称为git config,它允许你获得和设置配置变量:这些变量可以控制Git的外观和操作的各个方面. 一. 配置文件的存储位置 这些变量可以被存储在三个不同的位置: 1./etc/ ...
- Python3数据类型之数字
1. Python数字类型的作用 Python数字类型用来存储数值,它是不可变对象,一旦定义之后,其值不可以被修改.如果改变了数字类型的值,就要重新为其分配内存空间. 定义一个数字类型的变量:a = ...
- 开始使用 Manjaro(添加源+字体渲染去模糊+软件安装+优化配置+常见错误)(30)
1. 添加 archlinux 镜像源 1. 步骤一 向 /etc/pacman.d/mirrorlist 中添加国内镜像地址 1.1 方法1:自动添加 1. 输入如下命令查看国内镜像源,并按质量排序 ...
- triples I(按位或运算及3的特性)(2019牛客暑期多校训练营(第四场)D)
示例1: 输入: 2 3 7 输出: 1 32 3 6 说明:3=3, (3|6)=7 题意:输出尽可能少的数字,他们的按位或结果为输入的数字a. 题解:(表示看不懂题解,看山东大佬的代码看懂的)首先 ...
- MySQL DBA的KPI考核指标有哪些
绩效考核是对一名员工所作工作的数量.质量.难度.强度.效率的量化考量.由于DBA的工作性质与纯粹的研发人员或运维人员有所区别,对DBA的KPI考核指标也有其特殊性.参考以往的经验和一些较大的DBA t ...
- 使用docker-compose搭建WordPress
今天博主使用typecho各种不爽,索性干掉typecho,使用WordPress 依赖 mysql nginx yml 文件 version: '3' services: nginx: image: ...