发布于 2017-09-14 03:57:14 | 277 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


本篇文章主要介绍了Android利用Glide获取图片真正的宽高的实例,具有一定的参考价值,有兴趣的可以了解一下

前言

有时候需要获取网络图片的宽高来设置图片显示的大小,很多人会直接利用Glide的加载监听去拿图片的宽高,但是这样拿到的不是图片真正的宽高,而是图片显示在ImageView后的宽高。如下:


    //获取图片显示在ImageView后的宽高
    Glide.with(this)
        .load(imgUrl)
        .asBitmap()//强制Glide返回一个Bitmap对象
        .listener(new RequestListener<String, Bitmap>() {
          @Override
          public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
            Log.d(TAG, "onException " + e.toString());
            return false;
          }

          @Override
          public boolean onResourceReady(Bitmap bitmap, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Log.d(TAG, "width2 " + width); //400px
            Log.d(TAG, "height2 " + height); //400px
            return false;
          }
        }).into(mIv_img);

想要拿到图片真正的宽高,应该利用Glide的Target。如下:


    //获取图片真正的宽高
    Glide.with(this)
        .load(imgUrl)
        .asBitmap()//强制Glide返回一个Bitmap对象
        .into(new SimpleTarget<Bitmap>() {
          @Override
          public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Log.d(TAG, "width " + width); //200px
            Log.d(TAG, "height " + height); //200px
          }
        });

完整代码

MainActivity.java


public class MainActivity extends AppCompatActivity {

  private ImageView mIv_img;
  String imgUrl = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=523024675,1399288021&fm=117&gp=0.jpg";
  private String TAG = this.getClass().getSimpleName();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mIv_img = (ImageView) findViewById(R.id.iv_img);

    //获取图片真正的宽高
    Glide.with(this)
        .load(imgUrl)
        .asBitmap()//强制Glide返回一个Bitmap对象
        .into(new SimpleTarget<Bitmap>() {
          @Override
          public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Log.d(TAG, "width " + width); //200px
            Log.d(TAG, "height " + height); //200px
          }
        });

    //获取图片显示在ImageView后的宽高
    Glide.with(this)
        .load(imgUrl)
        .asBitmap()//强制Glide返回一个Bitmap对象
        .listener(new RequestListener<String, Bitmap>() {
          @Override
          public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
            Log.d(TAG, "onException " + e.toString());
            return false;
          }

          @Override
          public boolean onResourceReady(Bitmap bitmap, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Log.d(TAG, "width2 " + width); //400px
            Log.d(TAG, "height2 " + height); //400px
            return false;
          }
        }).into(mIv_img);
  }

}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  android:id="@+id/activity_main"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
    android:id="@+id/iv_img"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    android:scaleType="centerCrop"
    android:src="@mipmap/ic_launcher"/>

</RelativeLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务