zoukankan      html  css  js  c++  java
  • android讲义2之简单图片浏览器且有截图效果(对ImageView使用)

    布局特别之处:

    //与上一控件底部的距离

    android:layout_marginTop="10dp"

    代码阶段分析:

    //返回可画的视图,并用向下转型为Bitmap的可画视图

    BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();

    //如果图片还未回收,先强制回收该图片

    if (!bitmapDrawable.getBitmap().isRecycled())
    {
    bitmapDrawable.getBitmap().recycle();
    }

    //改变ImageView显示的图片
    image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), images[++currentImg]));

    //截取原来的图片进行显示

    1.使用setOnTouchListener监听

    2.获得屏幕的宽,用于第三步的取得实际比例

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    System.out.println(dm.widthPixels);

    3.获得bitmap图片实际大小与第一个ImageView的缩放比例

    double scale = bitmap.getWidth() /dm.widthPixels;

    4.获取需要显示的图片的开始点

    int x = (int) (event.getX() * scale);
    int y = (int) (event.getY() * scale);

    5.算法规定开始点加上显示区域不能大于图片区域

    if (x + 120 > bitmap.getWidth())
    {
    x = bitmap.getWidth() - 120;
    }
    if (y + 120 > bitmap.getHeight())
    {
    y = bitmap.getHeight() - 120;
    }

    6.显示图片的指定区域
    image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));

  • 相关阅读:
    时间日期总览
    Mysql一次更新多条数据
    windows远程桌面连接无法粘贴
    vmware workstation pro密钥
    C#自动生成XML文件
    Mysql 缺少MSVCR120DLL问题
    hdu 5672 Strings 模拟
    poj 1328 雷达覆盖 贪心
    hdu 5667 Sequence (矩阵快速幂)
    CodeForces 652D Nested Segments 树状数组
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2380078.html
Copyright © 2011-2022 走看看