图片(imageView)

时间:2023-03-08 20:03:59
图片(imageView)

图片(imageView):

常用属性:

android:scaleType(图片显示的格式)

android:src(图片源,一般使用的资源)

android:scaleType属性的常用取值

0.matrix:用矩阵来绘图

1.fitXY:拉伸图片(不按比列)以填充View的宽高

2.fitStart:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的上边

3.fitCenter:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间

4.fitEnd:按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的下边

5.center:按原图大小显示图片,但图片的宽高大于View的宽高时,截图图片中间部分显示

6.centerCrop:按比例放大原图直至等于某边View的宽高显示

7.centerInside:当原图宽高等于View的宽高的时候,按原图的大小居中显示,反之将原图缩放至view的宽高居中显示。

下面我们来看具体的代码实现:

1.Activity

//imageView显示图片视图
public class ImageViewActivity extends Activity { private Context context;
private ImageView imageView;
private Button previousButton;
private Button nextButton;
private int currentIndex = 0;
//这里你可以随便找一些图片来试试
private int[] images = new int[]{
R.drawable.shangjinlieren,
R.drawable.bulong,
R.drawable.buxiangzhiren,
R.drawable.gailun,
R.drawable.guanghuinvlang,
R.drawable.laoshu,
R.drawable.liulangfashi,
R.drawable.longxuewuji,
R.drawable.manzuzhiwang,
R.drawable.mishizhiya,
R.drawable.sha*angdi,
R.drawable.shengqiangyouxia,
R.drawable.shuguangnvshen,
R.drawable.wujijiansheng,
R.drawable.wushuangjianji,
R.drawable.zhaoxin
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_view); init();
addAction();
} private void init(){
context = this;
previousButton = (Button)findViewById(R.id.previousButtonId);
nextButton = (Button)findViewById(R.id.nextButtonId);
imageView = (ImageView)findViewById(R.id.imageViewId);
imageView.setImageResource(images[currentIndex]);
} private void addAction(){
previousButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(currentIndex==0){
Toast.makeText(context, "已经是第一张了...", Toast.LENGTH_SHORT).show();
}else{
currentIndex--;
imageView.setImageResource(images[currentIndex]);
setScaleType();
}
}
}); nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(currentIndex==images.length-1){
Toast.makeText(context, "已经是最后一张了...", Toast.LENGTH_SHORT).show();
}else{
currentIndex++;
imageView.setImageResource(images[currentIndex]);
setScaleType();
}
}
});
}
//图片显示的格式
private void setScaleType(){
if(currentIndex==0){
imageView.setScaleType(ImageView.ScaleType.MATRIX);
Toast.makeText(context, "ScaleType=MATRIX", Toast.LENGTH_SHORT).show();
}
if(currentIndex==1){
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Toast.makeText(context, "ScaleType=FIT_XY", Toast.LENGTH_SHORT).show();
}
if(currentIndex==2){
imageView.setScaleType(ImageView.ScaleType.FIT_START);
Toast.makeText(context, "ScaleType=FIT_START", Toast.LENGTH_SHORT).show();
}
if(currentIndex==3){
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Toast.makeText(context, "ScaleType=FIT_CENTER", Toast.LENGTH_SHORT).show();
}
if(currentIndex==4){
imageView.setScaleType(ImageView.ScaleType.FIT_END);
Toast.makeText(context, "ScaleType=FIT_END", Toast.LENGTH_SHORT).show();
}
if(currentIndex==5){
imageView.setScaleType(ImageView.ScaleType.CENTER);
Toast.makeText(context, "ScaleType=CENTER", Toast.LENGTH_SHORT).show();
}
if(currentIndex==6){
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Toast.makeText(context, "ScaleType=CENTER_CROP", Toast.LENGTH_SHORT).show();
}
if(currentIndex==7){
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Toast.makeText(context, "ScaleType=CENTER_INSIDE", Toast.LENGTH_SHORT).show();
}
} }

2.xml文件布局

<?xml version="1.0" encoding="utf-8"?>
<!-- 显示图片视图的页面 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" > <ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@color/yellow"
android:src="@drawable/mishizhiya" /> <ImageView
android:id="@+id/imageViewId"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@color/yellow" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:orientation="horizontal" > <Button
android:id="@+id/previousButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一张" /> <Button
android:id="@+id/nextButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout> </LinearLayout>

3.效果显示图:

图片(imageView)