Android實現(xiàn)的可以調(diào)整透明度的圖片查看器實例
更新時間:2014年07月22日 11:48:22 投稿:shichen2014
這篇文章主要介紹了Android實現(xiàn)的可以調(diào)整透明度的圖片查看器,需要的朋友可以參考下
本文以實例講解了基于Android的可以調(diào)整透明度的圖片查看器實現(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 { // 定義一個訪問圖片的數(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); // 定義查看下一張圖片的時間監(jiān)聽器 nextButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (currentImage >= 5) { currentImage = -1; } BitmapDrawable bitmap = (BitmapDrawable) imageview1 .getDrawable(); // 如果圖片還沒有回收,先強制回收圖片 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個按鈕添加監(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(); // 獲取第一個圖片顯示框中的位圖 Bitmap bitmap = bitmapDeaw.getBitmap(); double scale = bitmap.getWidth(); // 或許需要顯示圖片的開始點 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; } }); } }
運行效果圖如下:
相關(guān)文章
Android開發(fā)手冊TextInputLayout樣式使用示例
這篇文章主要為大家介紹了Android開發(fā)手冊TextInputLayout樣式使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06android獲取音樂文件的內(nèi)置專輯圖片實現(xiàn)思路及代碼
獲取音樂文件的內(nèi)置專輯圖片這是在播放音樂時的一個很不錯的功能,下面與大家分享下具體的實現(xiàn)思路,有類似需求的朋友可以參考下哈2013-06-06Android編程實現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-02-02Android基于PhotoView實現(xiàn)的頭像/圓形裁剪控件
這篇文章主要給大家介紹了關(guān)于Android基于PhotoView實現(xiàn)的頭像/圓形裁剪控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07