Android開(kāi)發(fā)實(shí)現(xiàn)的圖片瀏覽功能示例【放大圖片】
本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)的圖片瀏覽功能。分享給大家供大家參考,具體如下:
效果圖:

布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加透明度"
android:layout_weight="1"/>
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="減少透明度"
android:layout_weight="1"/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一張"
android:layout_weight="1"/>
</LinearLayout>
<!--此處顯示圖片整體-->
<ImageView
android:id="@+id/imagel"
android:layout_width="wrap_content"
android:layout_height="280dp"
android:src="@drawable/xiaochouyu"
android:scaleType="fitCenter"/>
<ImageView
android:id="@+id/image2"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#00f"
android:layout_margin="5dp"/>
</LinearLayout>
代碼實(shí)現(xiàn)透明度改變:
public class MainActivity extends AppCompatActivity {
//定義一個(gè)訪問(wèn)圖片的數(shù)組
int[] images = new int[]{
R.drawable.xiaochouyu ,
R.drawable.leidayu ,
R.drawable.paodangyu ,
R.drawable.huangjindiao ,
R.drawable.piaopiao
};
//定義默認(rèn)顯示的圖片
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 Button next = (Button) findViewById(R.id.next) ;
final ImageView imageView01 = (ImageView) findViewById(R.id.imagel);
final ImageView imageView02 = (ImageView) findViewById(R.id.image2);
//定義查看下一張圖片的監(jiān)聽(tīng)器
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//顯示下一張圖片
imageView01.setImageResource(images[currentImg++ % images.length]);
}
});
//定義改變圖片透明度的方法
View.OnClickListener listener = new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
if (v == plus){
alpha = alpha + 20 ;
}
if (v == minus){
alpha = alpha - 20 ;
}
if (alpha >= 255){
alpha = 255 ;
}
if (alpha <= 0){
alpha = 0 ;
}
imageView01.setImageAlpha(alpha);
}
};
//為兩個(gè)按鈕添加監(jiān)聽(tīng)器
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
imageView01.setOnTouchListener(new View.OnTouchListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onTouch(View v, MotionEvent event) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView01.getDrawable();
//獲取第一個(gè)托片顯示框中的位圖
Bitmap bitmap = bitmapDrawable.getBitmap();
//bitmap圖片實(shí)際大小與第一個(gè)Imageview的縮放比例
double scale = 1.0 * bitmap.getHeight() / imageView01.getHeight();
//獲取需要顯示的圖片開(kāi)始點(diǎn)
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 ;
}
//顯示圖片的指定區(qū)域
imageView02.setImageBitmap(Bitmap.createBitmap(bitmap , x , y , 120 , 120));
imageView02.setImageAlpha(alpha);
return false;
}
});
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
- Android開(kāi)發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作示例
- Android實(shí)現(xiàn)自定義滑動(dòng)刻度尺方法示例
- Android文字基線Baseline算法的使用講解
- Android開(kāi)發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例
- Android使用google breakpad捕獲分析native cash
- Android應(yīng)用動(dòng)態(tài)修改主題的方法示例
- Android樣式和主題之選擇器的實(shí)例講解
- Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例
- Android在ubuntu上過(guò)濾多條關(guān)鍵字日志
相關(guān)文章
基于Android中dp和px之間進(jìn)行轉(zhuǎn)換的實(shí)現(xiàn)代碼
本篇文章是對(duì)在Android中dp和px之間進(jìn)行轉(zhuǎn)換的實(shí)現(xiàn)方法進(jìn)行了分析介紹,需要的朋友參考下2013-05-05
詳解Android首選項(xiàng)框架的使用實(shí)例
首選項(xiàng)這個(gè)名詞對(duì)于熟悉Android的朋友們一定不會(huì)感到陌生,它經(jīng)常用來(lái)設(shè)置軟件的運(yùn)行參數(shù)。本篇文章主要介紹詳解Android首選項(xiàng)框架的使用實(shí)例,有興趣的可以了解一下。2016-11-11
Android實(shí)現(xiàn)Ant Design 自定義表單組件
Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,下面通過(guò)本文給大家詳細(xì)介紹Android實(shí)現(xiàn)Ant Design 自定義表單組件,需要的的朋友參考下吧2017-06-06
巧用ViewPager實(shí)現(xiàn)駕考寶典做題翻頁(yè)效果
本文主要介紹巧用ViewPager實(shí)現(xiàn)駕考寶典做題翻頁(yè)效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android開(kāi)發(fā)之StackView用法和遇到的坑分析
這篇文章主要介紹了Android開(kāi)發(fā)之StackView用法和遇到的坑,結(jié)合實(shí)例形式分析了Android StackView圖片操作用法及常見(jiàn)問(wèn)題解決方法,需要的朋友可以參考下2019-03-03
Android開(kāi)關(guān)控件Switch的使用案例
今天小編就為大家分享一篇關(guān)于Android開(kāi)關(guān)控件Switch的使用案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Android入門之TableLayout應(yīng)用解析(二)
這篇文章主要介紹了Android入門之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08

