Android控件之ImageView用法實(shí)例分析
本文實(shí)例講述了Android控件之ImageView用法。分享給大家供大家參考。具體如下:
ImageView控件是一個(gè)圖片控件,負(fù)責(zé)顯示圖片。
以下模擬手機(jī)圖片查看器
目錄結(jié)構(gòu):
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="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/p1"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:id="@+id/previous" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一張" android:layout_gravity="center_horizontal"/> <Button android:id="@+id/alpha_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度增加" android:layout_gravity="center_horizontal"/> <Button android:id="@+id/alpha_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度減少" android:layout_gravity="center_horizontal"/> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一張" android:layout_gravity="center_horizontal"/> </LinearLayout> </LinearLayout>
ImageViewActivity類:
package com.ljq.iv; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class ImageViewActivity extends Activity { private ImageView imageView=null; private Button previous=null;//上一張 private Button next=null;//下一張 private Button alpha_plus=null;//透明度增加 private Button alpha_minus=null;//透明度減少 private int currentImgId=0;//記錄當(dāng)前ImageView顯示的圖片id private int alpha=255;//記錄ImageView的透明度 int [] imgId = { //ImageView顯示的圖片數(shù)組 R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView=(ImageView)findViewById(R.id.imageView); previous=(Button)findViewById(R.id.previous); next=(Button)findViewById(R.id.next); alpha_plus=(Button)findViewById(R.id.alpha_plus); alpha_minus=(Button)findViewById(R.id.alpha_minus); previous.setOnClickListener(listener); next.setOnClickListener(listener); alpha_plus.setOnClickListener(listener); alpha_minus.setOnClickListener(listener); } private View.OnClickListener listener = new View.OnClickListener(){ public void onClick(View v) { if(v==previous){ currentImgId=(currentImgId-1+imgId.length)%imgId.length; imageView.setImageResource(imgId[currentImgId]); } if(v==next){ currentImgId=(currentImgId+1)%imgId.length; imageView.setImageResource(imgId[currentImgId]); } if(v==alpha_plus){ alpha+=10; if(alpha>255){ alpha=255; } imageView.setAlpha(alpha); } if(v==alpha_minus){ alpha-=10; if(alpha<0){ alpha=0; } imageView.setAlpha(alpha); } } }; }
運(yùn)行結(jié)果:
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- android imageview圖片居中技巧應(yīng)用
- Android控件系列之ImageView使用方法
- android ImageView 的幾點(diǎn)經(jīng)驗(yàn)總結(jié)
- Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
- Android使用控件ImageView加載圖片的方法
- Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放
- Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法
- Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)圓角矩形和圓形ImageView的方式
- Android編程實(shí)現(xiàn)ImageView圖片拋物線動(dòng)畫效果的方法
- Android開發(fā)實(shí)現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法
相關(guān)文章
Android?Studio實(shí)現(xiàn)智能聊天
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)智能聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android 自定義View實(shí)現(xiàn)任意布局的RadioGroup效果
這篇文章主要介紹了Android 自定義View實(shí)現(xiàn)任意布局的RadioGroup,需要的朋友可以參考下2018-11-11Android中Handler、Thread、HandlerThread三者的區(qū)別
本文主要介紹了Android中Handler、Thread、HandlerThread三者的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android實(shí)現(xiàn)高德地圖首頁(yè)效果(下)
這篇文章主要為大家詳細(xì)介紹了基于Android實(shí)現(xiàn)高德地圖首頁(yè)效果下篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-08-08Android開發(fā)之獲取網(wǎng)絡(luò)鏈接狀態(tài)
這篇文章主要介紹了Android獲取網(wǎng)絡(luò)鏈接狀態(tài)的方法,主要是通過ConnectivityManager類來完成的,需要的朋友可以參考下2014-08-08Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼
本篇文章主要介紹了Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01