欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中ImageView的使用方法

 更新時(shí)間:2021年05月21日 10:43:19   作者:nuist__NJUPT  
這篇文章主要為大家詳細(xì)介紹了Android中ImageView的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android中ImageView的使用:點(diǎn)擊按鈕,改變圖片透明度,切換圖片

布局是三個(gè)按鈕組件和一個(gè)ImageView組件

<?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"
    android:gravity="center">
    <Button
        android:id="@+id/addAlpha"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="增加圖片透明度"
        />
    <Button
        android:id="@+id/downAlpha"
        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/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="280dp"
        android:layout_marginTop="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/spring"
        />
</LinearLayout>

.java文件,控制增加和減少透明度以及圖片切換

public class MainActivity extends AppCompatActivity {
    private Button addAlpha;
    private Button downAlpha;
    private Button next;
    private ImageView imageView1;
    int [] images = new int[]{ //用數(shù)組存儲(chǔ)春,夏,秋,冬四張圖片
            R.drawable.spring,
            R.drawable.summer,
            R.drawable.fall,
            R.drawable.winter
    } ;
    int currentImage = 0 ; //定義當(dāng)前默認(rèn)顯示的圖片
    int alpha = 255 ;//定義圖片起始透明度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageview);
        addAlpha = (Button) findViewById(R.id.addAlpha);
        downAlpha = (Button) findViewById(R.id.downAlpha);
        next = (Button) findViewById(R.id.next);
        imageView1 = (ImageView) findViewById(R.id.imageView1);



        //對(duì)增加圖片透明度按鈕設(shè)置監(jiān)聽(tīng)事件
        addAlpha.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                if(alpha >= 255){ //255是透明度上線
                    alpha = 255 ;
                }
                else{ //每點(diǎn)擊增加透明度按鈕,透明度增加20
                    alpha += 20 ;
                }
                imageView1.setImageAlpha(alpha); //為圖片設(shè)置透明度
            }
        });

        //對(duì)減少透明度按鈕設(shè)置監(jiān)聽(tīng)事件
        downAlpha.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                if(alpha <= 0){ //透明度下限
                    alpha = 0 ;
                }
                else{
                    alpha -= 20 ;
                }
                imageView1.setImageAlpha(alpha); //為圖片設(shè)置透明度
            }
        });
        //對(duì)切換圖片按鈕設(shè)置監(jiān)聽(tīng)事件
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //控制顯示下一張圖片
                imageView1.setImageResource(images[++ currentImage % images.length]);
            }
        });
    }
}

效果圖如下,點(diǎn)擊按鈕可以改變透明度和切換圖片

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論