实例:
代码:
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<"
android:onClick="before"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:onClick="add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:onClick="sub"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">"
android:onClick="next"
/>
</LinearLayout>
<ImageView
android:layout_width="300dp"
android:layout_height="400dp"
android:id="@+id/iv_image_image"
android:background="#00ff00"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0000"
android:id="@+id/iv_image_new"/>
</LinearLayout>
Java代码
package com.example.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
/**
* Created by Administrator on 2017/6/6 0006.
*/
public class ImageActivity extends AppCompatActivity{
private ImageView iv_image_image;
private ImageView iv_image_new;
//内存中的图片(项目中的)
private int images[]={R.drawable.s1,R.drawable.s2,R.drawable.s3,R.drawable.s4,R.drawable.s5};
int currentIndex=0;
int currentAlpha=255;
//内存卡中的图片
private File[] files;
private Bitmap bm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
//获取图片控件
iv_image_image = (ImageView) findViewById(R.id.iv_image_image);
iv_image_new = (ImageView) findViewById(R.id.iv_image_new);
//设置初始化图片
//项目中的
// iv_image_image.setImageResource(images[currentIndex]);
//外部存储设备,内存卡中的
//1.判断内存卡是否可用
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//2.获取手机内存卡路径
String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
//3.获取手机内存卡目录中的图片
File file=new File(sdCardPath+"/images");
files=file.listFiles();
}
//内存卡中的
bm=BitmapFactory.decodeFile(files[0].getAbsolutePath());
iv_image_image.setImageBitmap(bm);
iv_image_image.setImageAlpha(currentAlpha);
//截取一部分(放大)
iv_image_image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//计算比例
float scaleX=(float) iv_image_image.getWidth()/bm.getWidth();
float scaleY=(float)iv_image_image.getHeight()/bm.getHeight();
//获取触摸时(控件的坐标)
float x=event.getX(); //宽
float y=event.getY(); //高
//实际位置
float x2=0;
float y2=0;
x2=x/scaleX;
y2=y/scaleY;
if(x2>bm.getWidth()||y2>bm.getHeight()){
Toast.makeText(ImageActivity.this,"超出了范围",Toast.LENGTH_SHORT).show();
}else{
if(x2+50>bm.getWidth()){
x2-=50;
}if(y2+50>bm.getHeight()){
y2-=50;
}
}
Bitmap bmNew=Bitmap.createBitmap(bm,(int)x2,(int)y2,50,50);
iv_image_new.setImageBitmap(bmNew);
return true;
}
});
}
public void before(View view){
currentIndex--;
if(currentIndex<0){
currentIndex=0;
Toast.makeText(ImageActivity.this,"兄弟,没有上一张了。",Toast.LENGTH_SHORT).show();
}
// iv_image_image.setImageResource(images[currentIndex]);
bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
iv_image_image.setImageBitmap(bm);
}
public void add(View view){
currentAlpha-=20;
if(currentAlpha<=0){
currentAlpha=0;
Toast.makeText(ImageActivity.this,"兄弟,看不见了。",Toast.LENGTH_SHORT).show();
}
iv_image_image.setImageAlpha(currentAlpha);
}
public void sub(View view){
currentAlpha+=20;
if(currentAlpha>=255){
currentAlpha=255;
Toast.makeText(ImageActivity.this,"兄弟,都给你了。",Toast.LENGTH_SHORT).show();
}
iv_image_image.setImageAlpha(currentAlpha);
}
public void next(View view){
currentIndex++;
if(currentIndex>=images.length){
currentIndex=images.length-1;
Toast.makeText(ImageActivity.this,"兄弟,没有下一张了。",Toast.LENGTH_SHORT).show();
}
// iv_image_image.setImageResource(images[currentIndex]);
bm=BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());
iv_image_image.setImageBitmap(bm);
}
}
一、常用属性
1、android:src 设置ImageView所显示的Drawable资源的Id
2、android:maxHeight 最大高度
3、android:maxWidth 最大宽度
4、android:adjustViewBounds 设置ImageView是否调整自己的大小所保持所显示图片的长宽比
5、android:scaleType 设置图片如何缩放以适应ImageView的大小
取值:
①matrix 使用矩阵的方式缩放
②fitXY 对图片横向纵向独立缩放,使得图片完全填充ImageView,可能会变形
③fitStart 保持长宽比,图片较长的边与ImageView对应边一致,然后放在左上角
④fitCenter 保持缩放比,图片较长的边与Imageview对应边一致,然后放在中间
⑤fitEnd 保持缩放比,图片较长的边与ImageView对应边一致,然后放在左下角
⑥center 图片放在中间,不缩放
⑦centerCrop 保持纵横比缩放,使得图片能完全覆盖ImageView
⑧centerInside 保持纵横比缩放,使得ImageView能完全显示图片
二、显示大图片
BitmapFactory.decodeFile(String pathName,Options opts);
三、从原有的图片中截取一部分作为新的图片
Bitmap newImage=Bitmap.createBitmap(oldImage,(int)rx,(int)ry,50,50);
第一个参数是原图片
第二、三个参数是从哪个坐标开始截取
第四、五个是截取多宽,多高