Android實(shí)現(xiàn)的可以調(diào)整透明度的圖片查看器實(shí)例
本文以實(shí)例講解了基于Android的可以調(diào)整透明度的圖片查看器實(shí)現(xiàn)方法,具體如下:
main.xml部分代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大透明度" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="減小透明度" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一張" />
</LinearLayout>
<!-- 定義顯示整體圖片的ImageView -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:scaleType="fitCenter"
android:src="@drawable/shuangta" />
<!-- 定義顯示局部圖片的ImageView -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#0000ff" />
</LinearLayout>
java部分代碼為:
package android.demo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidDemo5Activity extends Activity {
// 定義一個(gè)訪問圖片的數(shù)組
int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao,
R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi,
R.drawable.ic_launcher, };
// 定義當(dāng)前顯示的圖片
int currentImage = 2;
// 定義圖片的初始透明度
private int alpha = 255;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button plusButton = (Button) findViewById(R.id.button1);
final Button minuxButton = (Button) findViewById(R.id.button2);
final Button nextButton = (Button) findViewById(R.id.button3);
final ImageView imageview1 = (ImageView) findViewById(R.id.imageView1);
final ImageView imageview2 = (ImageView) findViewById(R.id.imageView2);
// 定義查看下一張圖片的時(shí)間監(jiān)聽器
nextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (currentImage >= 5) {
currentImage = -1;
}
BitmapDrawable bitmap = (BitmapDrawable) imageview1
.getDrawable();
// 如果圖片還沒有回收,先強(qiáng)制回收圖片
if (!bitmap.getBitmap().isRecycled()) {
bitmap.getBitmap().recycle();
}
// 改變ImageView的圖片
imageview1.setImageBitmap(BitmapFactory.decodeResource(
getResources(), images[++currentImage]));
}
});
// 定義改變圖片透明度的方法
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v == plusButton) {
alpha += 20;
}
if (v == minuxButton) {
alpha -= 20;
}
if (alpha > 255) {
alpha = 255;
}
if (alpha <= 0) {
alpha = 0;
}
// 改變圖片的透明度
imageview1.setAlpha(alpha);
}
};
// 為2個(gè)按鈕添加監(jiān)聽器
plusButton.setOnClickListener(listener);
minuxButton.setOnClickListener(listener);
imageview1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDeaw = (BitmapDrawable) imageview1
.getDrawable();
// 獲取第一個(gè)圖片顯示框中的位圖
Bitmap bitmap = bitmapDeaw.getBitmap();
double scale = bitmap.getWidth();
// 或許需要顯示圖片的開始點(diǎn)
int x = (int) (arg1.getX() * scale);
int y = (int) (arg1.getY() * scale);
if (x + 120 > bitmap.getWidth()) {
x = bitmap.getWidth() - 120;
}
if (y + 120 > bitmap.getHeight()) {
y = bitmap.getHeight() - 120;
}
// 顯示圖片的指定區(qū)域
imageview2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,
120, 120));
imageview2.setAlpha(alpha);
return false;
}
});
}
}
運(yùn)行效果圖如下:


- Android 簡單的圖片查看器源碼實(shí)現(xiàn)
- Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁源碼查看器
- android網(wǎng)絡(luò)圖片查看器簡單實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能
- Android仿微信朋友圈圖片查看器
- Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器實(shí)例
- Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
- Android仿百度圖片查看功能
相關(guān)文章
Android開發(fā)手冊TextInputLayout樣式使用示例
這篇文章主要為大家介紹了Android開發(fā)手冊TextInputLayout樣式使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
android獲取音樂文件的內(nèi)置專輯圖片實(shí)現(xiàn)思路及代碼
獲取音樂文件的內(nèi)置專輯圖片這是在播放音樂時(shí)的一個(gè)很不錯(cuò)的功能,下面與大家分享下具體的實(shí)現(xiàn)思路,有類似需求的朋友可以參考下哈2013-06-06
Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android中ProgressBar用法簡單實(shí)例
這篇文章主要介紹了Android中ProgressBar用法,以簡單實(shí)例形式分析了Android中ProgressBar進(jìn)度條控件的功能與布局相關(guān)技巧,需要的朋友可以參考下2016-01-01
解決django 多個(gè)APP時(shí) static文件的問題
這篇文章主要介紹了解決django 多個(gè)APP時(shí) static文件的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android編程實(shí)現(xiàn)自定義漸變顏色效果詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義漸變顏色效果,結(jié)合具體實(shí)例形式分析了Android基于xml及代碼定義來實(shí)現(xiàn)顏色漸變的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-08-08
Android編程實(shí)現(xiàn)通話錄音功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)通話錄音功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android廣播接收機(jī)制實(shí)現(xiàn)錄音功能的操作技巧,需要的朋友可以參考下2017-06-06
Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件
這篇文章主要給大家介紹了關(guān)于Android基于PhotoView實(shí)現(xiàn)的頭像/圓形裁剪控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

