Android开发实现的图片浏览功能示例【放大图片】

本文实例讲述了Android开发实现的图片浏览功能。分享给大家供大家参考,具体如下:

效果图:

布局文件:

<?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>

代码实现透明度改变:

public class MainActivity extends AppCompatActivity {
  //定义一个访问图片的数组
  int[] images = new int[]{
      R.drawable.xiaochouyu ,
      R.drawable.leidayu ,
      R.drawable.paodangyu ,
      R.drawable.huangjindiao ,
      R.drawable.piaopiao
  };
  //定义默认显示的图片
  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);
    //定义查看下一张图片的监听器
    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);
      }
    };
    //为两个按钮添加监听器
    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();
        //获取第一个托片显示框中的位图
        Bitmap bitmap = bitmapDrawable.getBitmap();
        //bitmap图片实际大小与第一个Imageview的缩放比例
        double scale = 1.0 * bitmap.getHeight() / imageView01.getHeight();
        //获取需要显示的图片开始点
        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 ;
        }
        //显示图片的指定区域
        imageView02.setImageBitmap(Bitmap.createBitmap(bitmap , x , y , 120 , 120));
        imageView02.setImageAlpha(alpha);
        return false;
      }
    });
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

(0)

相关推荐

  • Android实现自定义滑动刻度尺方法示例

    一 基础: 自定义View实现跟随手指滚动的刻度尺,实现了类似SeekBar的滑动选中效果.项目地址,欢迎star! UI图: 功能: 通过设置最小值跟最大值的范围,以及offset值.View将根据这些数据去计算出需要几个小刻度和几个长刻度,和每个长刻度上面显示的数值. 指针可以随意的定制. 当滑动停止后,刻度尺会根据四舍五入将距离指针最近的长刻度滑动到指针的位置. 支持范围越界回弹. 支持设置默认值. 二 实现: 先扯一下,再看别人写的控件的时候总有一种一脸懵逼的感觉,好多凌乱的变量和一大堆

  • Android点击Button实现切换点击图片效果的示例

    这是一个简单的小的DEMO , 关于点击按钮用于实现图片的切换, 重要的就是里面的关于逻辑的处理, 在以后图片轮播的技术上关于逻辑的处理和这个类似 Android Button的点击事件切换点击图片 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:la

  • Android应用动态修改主题的方法示例

    1.使用API设置主题 如下所示,在Activity中使用setTheme setTheme(R.style.MyTheme1); 2.调用API的时机 需要在super.onCreate(savedInstanceState)之前调用setTheme 3.重新设置主题 要重新设置主题,则需要结束Activity,重新启动才可以 public class MainActivity extends Activity { private TextView tv; @Override protecte

  • Android使用google breakpad捕获分析native cash

    Android 开发高手课 课后练习(1) 一.Chapter01 崩溃 https://time.geekbang.org/column/article/70602 https://github.com/AndroidAdvanceWithGeektime/Chapter01 1.遇到native cash时,生成.dmp文件 先检查sdk/ndk环境 在local.properties配置sdk/ndk 打包运行效果 点击CRASH按钮后生成的.dmp文件 2.利用breakpad的mini

  • Android单项绑定MVVM项目模板的方法

    0.前言 事情还要从上周和同事的小聚说起,同事说他们公司现在app的架构模式用的是MVP模式,但是并没有通过泛型和继承等一些列手段强制使用,全靠开发者在Activity或者Fragment里new一个presenter来做处理,说白了,全靠开发者自觉.这引发了我的一个思考,程序的架构或者设计模式的作用,除了传统的做到低耦合高内聚,业务分离,我觉得还有一个更重要的一点就是用来约束开发者,虽然使用某种模式或者架构可能并不会节省代码量,有的甚至会增加编码工作,但是让开发者在一定规则内进行开发,保证一个

  • Android在ubuntu上过滤多条关键字日志

    1 问题 比如我们在查问题的时候,需要过滤多个关键字,我平时的做法是一个终端执行下面的命令,然后几个关键字就几个终端,切换来切换去不方便看日志 adb logcat | grep **** 2 改进办法 今天看到同事用了grep -E,我们可以通过-E这个参数过滤多个关键字,比如 adb logcat | grep -E "word1 | word2" 还比如过滤3个 adb logcat | grep -E "word1 | word2 | word3" 老铁们,

  • Android文字基线Baseline算法的使用讲解

    引言 Baseline是文字绘制时所参照的基准线,只有先确定了Baseline的位置,我们才能准确的将文字绘制在我们想要的位置上.Baseline的概念在我们使用TextView等系统控件直接设置文字内容时是用不到的,但是如果我们想要在Canvas画布上面绘制文字时,Baseline的概念就必不可少了. 我们先了解一下Android中Canvas画布绘制文字的方法,如下图: 参数示意: text,文字内容 x,文字从画布上开始绘制的x坐标(Canvas是一个原点在左上角的平面坐标系) y,Bas

  • Android样式和主题之选择器的实例讲解

    Android学习笔记之样式和主题之选择器 (1)布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_

  • Android开发实现ListView和adapter配合显示图片和文字列表功能示例

    本文实例讲述了Android开发实现ListView和adapter配合显示图片和文字列表功能.分享给大家供大家参考,具体如下: 实际效果: 布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sc

  • Android开发之ListView的简单用法及定制ListView界面操作示例

    本文实例讲述了Android开发之ListView的简单用法及定制ListView界面操作.分享给大家供大家参考,具体如下: 效果: 如何从获得listview上item的内容 详见:https://www.jb51.net/article/158000.htm 中遇到的问题部分. 布局实现: 有个listview显示 一个edit和button发送 <?xml version="1.0" encoding="utf-8"?> <RelativeL

随机推荐