您的位置:首页 > 移动开发 > Android开发

Android控件之ImageView

2015-08-24 20:57 441 查看

1.ImageView

ImageView控件是一个图片控件,负责显示图片。

首先要把图片放到src>>res>>xhdpi文件夹下才能使用。

ScaleType的值分别代表的意义:

ImageView是Android中的基础图片显示控件,该控件有个重要的属性是ScaleType,该属性用以表示显示图片的方式,共有8种取值
    ScaleType.CENTER::图片大小为原始大小,如果图片大小大于ImageView控件,则截取图片中间部分,若小于,则直接将图片居中显示。
    ScaleType.CENTER_CROP:将图片等比例缩放,让图像的短边与ImageView的边长度相同,即不能留有空白,缩放后截取中间部分进行显示。
    ScaleType.CENTER_INSIDE:将图片大小大于ImageView的图片进行等比例缩小,直到整幅图能够居中显示在ImageView中,小于ImageView的图片不变,直接居中显示。
   ScaleType.FIT_CENTER:ImageView的默认状态,大图等比例缩小,使整幅图能够居中显示在ImageView中,小图等比例放大,同样要整体居中显示在ImageView中。
   ScaleType.FIT_END:缩放方式同FIT_CENTER,只是将图片显示在右方或下方,而不是居中。
   ScaleType.FIT_START:缩放方式同FIT_CENTER,只是将图片显示在左方或上方,而不是居中。
  ScaleType.FIT_XY:将图片非等比例缩放到大小与ImageView相同。
  ScaleType.MATRIX:是根据一个3x3的矩阵对其中图片进行缩放


2、举例分析

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"//将图片非等比例缩放到大小与ImageView相同。效果和background一样
            android:src="@mipmap/meinv"
            android:tint="#99ffcccc" />//添加蒙版,前面两个数是设置透明度

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@mipmap/meinv"
            android:src="@mipmap/meinv" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
       //设置两个调节透明度的按钮
            <Button
                android:id="@+id/image_add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="增加透明度" />

            <Button
                android:id="@+id/image_cut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="减少透明度" />
        </LinearLayout>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@mipmap/meinv" />
        <ImageView
            android:layout_width="match_parent"//宽度占满
            android:layout_height="match_parent"
            android:scaleType="fitEnd"//在后面
            android:src="@mipmap/shuai" />
    </LinearLayout>


在imageActivity中的部分代码

public class imageActivity extends AppCompatActivity implements View.OnClickListener {//注意implements View.OnClickListener 

    private ImageView mImage;
    private Button mImageAdd;
    private Button mImageCut;
    private int mAlphaCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imageview);
        mImageAdd = (Button)findViewById(R.id.image_add);
        mImageCut = (Button)findViewById(R.id.image_cut);
        mImageAdd.setOnClickListener(this);
        mImageCut.setOnClickListener(this);
        mImage = (ImageView)findViewById(R.id.image);
        mImage.setImageResource(R.mipmap.meinv2);//更换图片
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        int code = Build.VERSION.SDK_INT;//得到当前Android sdk的版本
        switch(v.getId()){
            case R.id.image_add:
                mAlphaCount+=5;//透明度加5
                Log.d("wersioncode", "当前版本:" + code);
                if(code<16){
                    mImage.setAlpha(mAlphaCount);
                }else{
                    mImage.setImageAlpha(mAlphaCount);
                }
                break;
            case R.id.image_cut:
                mAlphaCount-=5;
                if(code<16){
                    mImage.setAlpha(mAlphaCount);
                }else{
                    mImage.setImageAlpha(mAlphaCount);
                }
        }
    }


最终结果

[视频展示]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: