android 常用控件--ImageView

来源:互联网 发布:软件开发培训多久 编辑:程序博客网 时间:2024/06/18 10:53


   一、ImageView常用属性

      常用属性:android:src   设置ImageView所显示的Drawable资源的ID

                  android:maxHeight    最大高度

                           android:maxWidth     最大宽度

                   android:abjustViewBounds   设置ImageView是否调整自己的大小保持所显示图片的长宽比

                           android:scaleType     设置图片如何缩放以适应imageView的大小

                                                               取值:fitXY    对图片横向纵向独立缩放,使得图片完全填充ImageView,可能会变形

                                                                       fitStart   保持长宽比,图片较长边与ImageView对应边一致,然后放在左上角

                                                                       fitCenter 保持长宽比,图片较长边与ImageView对应边一致,然后放在中间

                                                                       fitEnd     保持长宽比,图片较长边与ImageView对应边一致,然后放在右下角

                                                                       center     图片放在中间不缩放

                                                                      centerCrop    保存纵横比缩放  使得完全覆盖ImageView

                                                                      centerInside   保存纵横比缩放  使得ImageView能完全显示图片


二、实现图片的上一张,下一张,透明度

                 activity_main.xml文件代码如下

                 

<?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"        android:orientation="horizontal"        >        <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="#ff0000"        />    <ImageView        android:layout_width="100dp"        android:layout_height="100dp"        android:background="#ff0000"        android:id="@+id/iv_image_new"        /></LinearLayout>

    

                        MainActivity.java文件代码如下

package com.zking.g160628_android06_widget3;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;/** *  */public class ImageActivity extends AppCompatActivity {    //Ctrl+Alt+F    private ImageView iv_image_image;    int currentIndex=0;    int currentAlpha=255;    private File[] files;    private Bitmap bm;    private ImageView iv_image_new;    @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);        //判断 手机是否有内存卡 内存卡是否可用        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){            //获取手机内存卡路径            String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();            //获取手机内存卡目录中的图片            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);    }    public void before(View view){        currentIndex--;        if(currentIndex<0){            currentIndex=0;            Toast.makeText(ImageActivity.this, "第一张", Toast.LENGTH_SHORT).show();        }        bm= BitmapFactory.decodeFile(files[currentIndex].getAbsolutePath());        iv_image_image.setImageBitmap(bm);    }    public void next(View view){        currentIndex++;        if(currentIndex>=files.length){            currentIndex=files.length-1;            Toast.makeText(ImageActivity.this, "最后一张", Toast.LENGTH_SHORT).show();        }        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);    }}


         

       

原创粉丝点击