android 常用控件--ImageView


   一、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);
    }


}


         

       

發佈了41 篇原創文章 · 獲贊 1 · 訪問量 6764
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章