安卓案例-圖片瀏覽器

本例中的圖片瀏覽器可以改變所查看的圖片的透明度,可通過調用ImageView 的setImageAlpha方法來實現,不僅如此,這個圖片瀏覽器還可以通過一個小區域來查看圖片的原始大小,因此本案例定義兩個ImageView ,一個用於查看圖片整體,一個用於查看圖片局部的細節。

本案例效果圖如下

image

佈局文件代碼如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="pub.weber.bym.imageview.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center|top">
        <Button
            android:id="@+id/plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增大透明度"/>
        <Button
            android:id="@+id/minus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="減小透明度"/>
        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一張"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="280dp"
        android:src="@drawable/b"
        android:scaleType="fitCenter"/>
    <ImageView
        android:id="@+id/image2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="#00f"
        android:layout_margin="10dp"/>
</LinearLayout>

MainActivity 代碼如下

public class MainActivity extends AppCompatActivity {
    // 圖片數組
    int [] images = new int[]{
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d
    };
    int currentImg = 2;
    private int alpha = 255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button plus = (Button) findViewById(R.id.plus);
        final Button minus = (Button) findViewById(R.id.minus);
        final ImageView image1 = (ImageView) findViewById(R.id.image1);
        final ImageView image2 = (ImageView) findViewById(R.id.image2);
        final Button next = (Button) findViewById(R.id.next);
        // 查看下張圖片
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image1.setImageResource(images[++currentImg % images.length]);
            }
        });
        // 改變透明度方法
        View.OnClickListener listener = new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if (view == plus){
                    alpha += 20;
                }
                if (view == minus){
                    alpha -= 20;
                }
                if (alpha >= 255){
                    alpha = 255;
                }
                if (alpha <= 0){
                    alpha = 0;
                }
                image1.setImageAlpha(alpha);
            }
        };
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
        image1.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent event){
                BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
                // 獲取第一個圖片顯示框中的位圖
                Bitmap bitmap = bitmapDrawable.getBitmap();
                // bitmap 圖片實際大小與第一個 ImageView 的縮放比例
                double scale = 1.0 * bitmap.getHeight() / image1.getHeight();
                // 獲取需要顯示的圖片的開始點
                int x = (int) (event.getX() * scale);
                int y = (int) (event.getY() * scale);
                if (x + 120 > bitmap.getWidth()){
                    x = bitmap.getWidth() - 120;
                }
                if (y + 120 > bitmap.getHeight()){
                    y = bitmap.getHeight() -120;
                }
                // 顯示圖片的指定區域
                image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120));
                image2.setImageAlpha(alpha);
                return false;
            }
        });
    }
}
發佈了130 篇原創文章 · 獲贊 39 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章