简体   繁体   English

如何以编程方式调整ImageView的大小?

[英]how to resize an ImageView Programmatically?

I'm beginner in android and i have problem in resize image. 我是Android的初学者,我在调整大小图像时遇到问题。 size of image change every time(check that by line of print), but that not appear in actual image. 图像的大小每次都会改变(通过打印行检查),但不会出现在实际图像中。

my code: 我的代码:

   import android.os.Bundle;
    import android.app.Activity;
    import android.content.DialogInterface.OnClickListener;
    import android.view.Menu;
    import android.view.View;
    import android.view.animation.*;
    import android.widget.Button;
    import android.widget.ImageView;

    public class MainActivity extends Activity {

        public static int state = 2;
        public static ImageView image;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            image = (ImageView) findViewById(R.id.imageView1);

            final Button button2 = (Button) findViewById(R.id.button2);
            button2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    System.out.println("1- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                    image.getLayoutParams().height += 20;
                    image.getLayoutParams().width += 20;
                    System.out.println("2- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                }
            });
        }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    }

You need to notify UI that your view bounds have changed. 您需要通知UI您的视图边界已更改。 Just add this line image.requestLayout(); 只需添加此行image.requestLayout(); below you println method and it should work: println方法下面它应该工作:

       button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                System.out.println("1- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                image.getLayoutParams().height += 20;
                image.getLayoutParams().width += 20;
                System.out.println("2- "+image.getLayoutParams().height+", "+image.getLayoutParams().width);
                image.requestLayout();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM