Android实现的可以调整透明度的图片查看器实例

 更新时间:2014年07月22日 11:48:22   投稿:shichen2014  
这篇文章主要介绍了Android实现的可以调整透明度的图片查看器,需要的朋友可以参考下

本文以实例讲解了基于Android的可以调整透明度的图片查看器实现方法,具体如下:

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 {
  // 定义一个访问图片的数组
  int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao,
      R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi,
      R.drawable.ic_launcher, };
  // 定义当前显示的图片
  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);

    // 定义查看下一张图片的时间监听器
    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个按钮添加监听器
    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;
        }

        // 显示图片的指定区域
        imageview2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y,
            120, 120));
        imageview2.setAlpha(alpha);
        return false;
      }
    });
  }

}

运行效果图如下:

相关文章

  • Android批量插入数据到SQLite数据库的方法

    Android批量插入数据到SQLite数据库的方法

    这篇文章主要为大家详细介绍了Android批量插入数据到SQLite数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android中AutoCompleteTextView与TextWatcher结合小实例

    Android中AutoCompleteTextView与TextWatcher结合小实例

    这篇文章主要为大家详细介绍了Android中AutoCompleteTextView与TextWatcher结合的小实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android APP存活检测方式

    Android APP存活检测方式

    这篇文章主要介绍了Android APP存活检测方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android hid发送apdu格式数据示例详解

    Android hid发送apdu格式数据示例详解

    这篇文章主要介绍了Android hid发送apdu格式数据,在 Android 中,如果你想通过 HID(Human Interface Device)发送 APDU 格式的数据,通常会涉及 USB HID 设备或蓝牙 HID 设备,本文给大家讲解的非常详细,需要的朋友可以参考下
    2023-08-08
  • Android ViewPager自定义轮播图并解决播放冲突

    Android ViewPager自定义轮播图并解决播放冲突

    这篇文章主要为大家详细介绍了Android ViewPager自定义轮播图并解决播放冲突,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Android音视频开发之VideoView使用指南

    Android音视频开发之VideoView使用指南

    VideoView组件内部同样是使用MediaPlayer+SurfaceView的形式控制MediaPlayer对视频文件进行播放,本文就来详细讲讲它的使用方法,需要的可以参考一下
    2022-04-04
  • 打造酷炫的AndroidStudio插件

    打造酷炫的AndroidStudio插件

    这篇文章主要为大家详细介绍了如何打造酷炫的AndroidStudio插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android View实现圆形进度条

    Android View实现圆形进度条

    这篇文章主要为大家详细介绍了Android View实现圆形进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 如何给Android中的按钮添加图片功能

    如何给Android中的按钮添加图片功能

    这篇文章主要介绍了Android中的按钮加图片功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • Android中极简的js与java的交互库(SimpleJavaJsBridge)

    Android中极简的js与java的交互库(SimpleJavaJsBridge)

    本文主要介绍了Android中极简的js与java的交互库--SimpleJavaJsBridge,它可以让js与java之间的通信更简单。 具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01

最新评论