我如何裁剪位图ImageView的? [英] How can I crop a bitmap for ImageView?

查看:103
本文介绍了我如何裁剪位图ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这应该是简单,但安卓scaleType =centerCrop不裁剪图像

我得到了图像的 1950年像素的宽,需要通过父母的宽度被裁剪。但安卓scaleType =centerCrop不裁剪图像。我需要什么做的布局,只显示第一个的 400像素的,例如或任何画面/父宽度

抱歉简单的问题 - 试图谷歌它 - 只有复杂的问题存在。而我是新的,所以不要downvote请)

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / RL1
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@色/ background_color>

    < ImageView的
            机器人:ID =@ + ID / ver_bottompanelprayer
            机器人:layout_width =match_parent
            机器人:layout_height =227px
            机器人:layout_alignParentBottom =真
            机器人:layout_alignParentLeft =真
            机器人:scaleType =矩阵

            机器人:背景=@可绘制/ ver_bottom_panel_tiled_long/>

< / RelativeLayout的>
 

如果有唯一的办法就是programmaticaly裁剪 - 请给我一个建议与方法

解决方案

好吧,我会糊的评论,因为答案:) - >

  RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.rl1);

最后的选项bitmapOptions =新的选项();
bitmapOptions.inDensity =采样大小;
bitmapOptions.inTargetDensity = 1;

/ *`final`改性剂可能是必要的位图* /
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.ver_bottom_panel_tiled_long,bitmapOptions);
bmp.setDensity(Bitmap.DENSITY_NONE);
BMP = Bitmap.createBitmap(BMP,0,0,rl.getWidth(),bmp.getHeight());
 

 然后在code:
 

  ImageView的IV =(ImageView的)v.findViewById(R.id.ver_bottompanelprayer);
如果(IV!= NULL){
  iv.setImageBitmap(BMP);
}
 

干杯:)

I know this should be simple , but android:scaleType="centerCrop" doesn't crop Image

I got image 1950 pixels wide and need it to be cropped by parent's width. But android:scaleType="centerCrop" doesn't crop Image. What do I need to do in layout to show only first 400 pixels, for instance or whatever screen/parent width is

Sorry for simple question - tried to Google it - only complicated questions there. And I'm new, so don't downvote please)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color">

    <ImageView
            android:id="@+id/ver_bottompanelprayer"
            android:layout_width="match_parent"
            android:layout_height="227px"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:scaleType="matrix"

            android:background="@drawable/ver_bottom_panel_tiled_long" />

</RelativeLayout>

if there's only way is to programmaticaly crop it - please give me an advice with a method

解决方案

Alright, I will paste the comment as answer :) ->

RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl1);

final Options bitmapOptions=new Options();
bitmapOptions.inDensity=sampleSize;
bitmapOptions.inTargetDensity=1;

/*`final` modifier might be necessary for the Bitmap*/
Bitmap bmp= BitmapFactory.decodeResource(getResources(), R.drawable.ver_bottom_panel_tiled_long, bitmapOptions);
bmp.setDensity(Bitmap.DENSITY_NONE);
bmp = Bitmap.createBitmap(bmp, 0, 0, rl.getWidth(), bmp.getHeight());

Then in the code:

ImageView iv = (ImageView)v.findViewById(R.id.ver_bottompanelprayer);
if (iv != null){
  iv.setImageBitmap(bmp);
}

Cheers :)

这篇关于我如何裁剪位图ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆