Android中ImageView的使用方法
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)文章
Android如何創(chuàng)建自定義ActionBar
這篇文章主要教大家如何創(chuàng)建自定義的ActionBar,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android自定義控件實(shí)現(xiàn)簡(jiǎn)單寫字板功能
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)簡(jiǎn)單寫字板功能的相關(guān)資料,需要的朋友可以參考下2016-04-04Android 模擬器(JAVA)與C++ socket 通訊 分享
Android 模擬器(JAVA)與C++ socket 通訊 分享,需要的朋友可以參考一下2013-05-05Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽(tīng)詳解
本文主要介紹了Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽(tīng)的方法步驟。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01Android跳轉(zhuǎn)三方應(yīng)用實(shí)例代碼
大家好,本篇文章主要講的是Android跳轉(zhuǎn)三方應(yīng)用實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android OpenGL入門之GLSurfaceView
這篇文章主要介紹了OpenGL入門知識(shí),如何在Android中使用GLSurfaceView,如果對(duì)OpenGL感興趣的同學(xué),可以參考下2021-04-04Android Studio4.0解決Gradle下載超時(shí)問(wèn)題
這篇文章主要介紹了Android Studio4.0解決Gradle下載超時(shí)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10詳解Android開(kāi)發(fā)技巧之PagerAdapter實(shí)現(xiàn)類的封裝
這篇文章主要介紹了詳解Android開(kāi)發(fā)技巧之PagerAdapter實(shí)現(xiàn)類的封裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11