我需要大小的ImageView我的表行内编程 [英] I need to size the imageView inside my table row programatically

查看:111
本文介绍了我需要大小的ImageView我的表行内编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的的ImageView 调整,但它并没有或者通过位图或其他方法工作,我要去把我的code,所以如果能谁能帮助我如何大小的ImageView 来适应表行内日Thnx

 公共类测试扩展活动
{
    私人TableLayout表1;
    保护无效的onCreate(捆绑savedInstanceState)
    {
        INT [] = ImageArray {R.raw.hospital_image,R.raw.hotel_image};
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.favorites);
        表1 =(TableLayout)findViewById(R.id.Table);
        TableRow.LayoutParams tableRowParams =新TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT,1.0F);
        的for(int i = 0; I< ImageArray.length;我++)
        {
            TR的TableRow =新的TableRow(本);
            TR.setLayoutParams(tableRowParams);
            ImageView的IMG =新ImageView的(本);
            //我应该怎么办这里来调整它?
            。可绘制D = getResources()getDrawable(ImageArray [I]);
            img.setImageDrawable(四);            TR.addView(IMG,新TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT,1.0F));            Table1.addView(TR);
        }
    }

我的XML仅持有这些:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <滚动型
        机器人:ID =@ + ID / scrollView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真正的>        < TableLayout
            机器人:ID =@ + ID /表1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>
        < / TableLayout>
    < /滚动型>< / RelativeLayout的>


解决方案

试试这个,

 公共类ResizableImageView扩展了ImageView的{
    公共ResizableImageView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    公共ResizableImageView(上下文的背景下){
        超级(上下文);
    }    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
        可绘制D = getDrawable();
        如果(D == NULL){
            super.setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
            返回;
        }        INT imageHeight = d.getIntrinsicHeight();
        INT imageWidth = d.getIntrinsicWidth();        INT widthSize = MeasureSpec.getSize(widthMeasureSpec);
        INT heightSize = MeasureSpec.getSize(heightMeasureSpec);        浮imageRatio = 0.0F;
        如果(imageHeight大于0){
            imageRatio = imageWidth / imageHeight;
        }
        浮sizeRatio = 0.0F;
        如果(heightSize大于0){
            sizeRatio = widthSize / heightSize;
        }        INT宽度;
        INT高度;
        如果(imageRatio> = sizeRatio){
            //设置宽度为最大允许
            宽度= widthSize;
            //标高
            高度=宽* imageHeight / imageWidth;
        }其他{
            //允许设置高度为最大
            高度= heightSize;
            //宽规模
            宽度=身高* imageWidth / imageHeight;
        }        setMeasuredDimension(宽度,高度);
    }
}

用它在你的布局,你会普通的旧的ImageView ,只是改变标签,这样,

 < com.example.ResizableImageView
            机器人:ID =@ + ID /图像
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:adjustViewBounds =真
            机器人:填充=1像素
            机器人:scaleType =fitCenter
            机器人:SRC =.../>

I've tried to set my ImageView resized but it did not work either by bitmap or other methods I'm going to place my code so if can anyone help me how to size the ImageView to fit inside the table row thnx.

public class Test extends Activity
{
    private TableLayout Table1;
    protected void onCreate(Bundle savedInstanceState) 
    { 
        int[] ImageArray={R.raw.hospital_image,R.raw.hotel_image};
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorites);
        Table1 = (TableLayout) findViewById(R.id.Table);
        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
        for(int i=0;i<ImageArray.length;i++)
        {
            TableRow TR = new TableRow(this);
            TR.setLayoutParams(tableRowParams);
            ImageView img=new ImageView(this);
            //What should i do here to resize it ???
            Drawable d = getResources().getDrawable(ImageArray[i]);
            img.setImageDrawable(d); 

            TR.addView(img, new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1.0f));

            Table1.addView(TR);
        }
    }

My XMl only holds these :

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

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <TableLayout
            android:id="@+id/Table1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableLayout>
    </ScrollView>

</RelativeLayout>

解决方案

try this,

public class ResizableImageView extends ImageView {
    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ResizableImageView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();
        if (d == null) {
            super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        int imageHeight = d.getIntrinsicHeight();
        int imageWidth = d.getIntrinsicWidth();

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        float imageRatio = 0.0F;
        if (imageHeight > 0) {
            imageRatio = imageWidth / imageHeight;
        }
        float sizeRatio = 0.0F;
        if (heightSize > 0) {
            sizeRatio = widthSize / heightSize;
        }

        int width;
        int height;
        if (imageRatio >= sizeRatio) {
            // set width to maximum allowed
            width = widthSize;
            // scale height
            height = width * imageHeight / imageWidth;
        } else {
            // set height to maximum allowed
            height = heightSize;
            // scale width
            width = height * imageWidth / imageHeight;
        }

        setMeasuredDimension(width, height);
    }
}

use it in your layout as you would plain old ImageView, just change the tag, like this,

        <com.example.ResizableImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:padding="1px"
            android:scaleType="fitCenter"
            android:src="..." />

这篇关于我需要大小的ImageView我的表行内编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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