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

 更新时间:2019年04月03日 14:26:12   作者:水中鱼之1999  
这篇文章主要介绍了Android开发实现的图片浏览功能,结合实例形式分析了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程序设计有所帮助。

相关文章

  • 基于Android中dp和px之间进行转换的实现代码

    基于Android中dp和px之间进行转换的实现代码

    本篇文章是对在Android中dp和px之间进行转换的实现方法进行了分析介绍,需要的朋友参考下
    2013-05-05
  • Android开发菜单布局之表格布局示例

    Android开发菜单布局之表格布局示例

    这篇文章主要介绍了Android开发菜单布局之表格布局,结合具体实例形式分析了Android菜单布局中表格布局的相关行列排版与设置操作技巧,需要的朋友可以参考下
    2019-04-04
  • 详解Android首选项框架的使用实例

    详解Android首选项框架的使用实例

    首选项这个名词对于熟悉Android的朋友们一定不会感到陌生,它经常用来设置软件的运行参数。本篇文章主要介绍详解Android首选项框架的使用实例,有兴趣的可以了解一下。
    2016-11-11
  • Android实现Ant Design 自定义表单组件

    Android实现Ant Design 自定义表单组件

    Ant Design 组件提供了Input,InputNumber,Radio,Select,uplod等表单组件,下面通过本文给大家详细介绍Android实现Ant Design 自定义表单组件,需要的的朋友参考下吧
    2017-06-06
  • 巧用ViewPager实现驾考宝典做题翻页效果

    巧用ViewPager实现驾考宝典做题翻页效果

    本文主要介绍巧用ViewPager实现驾考宝典做题翻页效果的实例,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • Android开发之StackView用法和遇到的坑分析

    Android开发之StackView用法和遇到的坑分析

    这篇文章主要介绍了Android开发之StackView用法和遇到的坑,结合实例形式分析了Android StackView图片操作用法及常见问题解决方法,需要的朋友可以参考下
    2019-03-03
  • Android开关控件Switch的使用案例

    Android开关控件Switch的使用案例

    今天小编就为大家分享一篇关于Android开关控件Switch的使用案例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Android仿ios加载loading菊花图效果

    Android仿ios加载loading菊花图效果

    这篇文章主要介绍了Android仿ios加载loading菊花图效果,本文通过实例代码效果图展示给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • Android入门之TableLayout应用解析(二)

    Android入门之TableLayout应用解析(二)

    这篇文章主要介绍了Android入门之TableLayout应用,需要的朋友可以参考下
    2014-08-08
  • Flutter应用框架搭建之屏幕适配详解

    Flutter应用框架搭建之屏幕适配详解

    因移动设备的多样性,特别是 Android 的碎片化严重,存在各种各样的分辨率,而 Flutter 跨平台开发又需同时支持 Android 和 iOS ,为尽可能的还原设计图效果提升用户体验,屏幕适配就势在必行了。本文将详细讲解Flutter屏幕适配的方法,需要的可以参考一下
    2022-03-03

最新评论