简体   繁体   English

单击按钮加载图像

[英]Loading an image with a button click

I have this button which loads an image from an URL. 我有这个按钮可以从URL加载图像。 I tried setting the ImageView visibility to Invisible so when I start the app the image doesn't show up until I click the button. 我尝试将ImageView可见性设置为Invisible,所以当我启动该应用程序时,直到单击该按钮,图像才会显示出来。 But I want to know, is there any other way? 但我想知道,还有其他办法吗? Any other way to make it work without setting the ImageView's visibility to invisible . 不将ImageView的可见性设置为invisible的情况下使其工作的任何其他方法。

Here's my code: 这是我的代码:

public class MainActivity extends Activity {

    String url = "http://test.please.do/app/car.jpg";
    ImageView imageView;
    Button buttonPlay;
    Button buttonFullScreen;
    Button buttonLoad;
    static final int REQUEST_VIDEO_CAPTURE = 1;
    VideoView resultvideo;
    MediaController mediacontroller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        resultvideo = (VideoView)findViewById(R.id.videoView);
        mediacontroller = new MediaController(MainActivity.this);

        mediacontroller.setAnchorView(resultvideo);

        resultvideo.setMediaController(mediacontroller);

        Button click = (Button)findViewById(R.id.buttonRecord);
        resultvideo = (VideoView)findViewById(R.id.videoView);

        final ImageView url = (ImageView) findViewById(R.id.imageView);
        Picasso.with(this).load("http://test.please.do/app/car.jpg").into(url);

        buttonLoad = (Button) findViewById(R.id.buttonLoad);
        {
            buttonLoad.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    url.setVisibility(View.VISIBLE);
                }
            });
        }
    }

change 更改

final ImageView url = (ImageView) findViewById(R.id.imageView);
    Picasso.with(this).load("http://test.please.do/app/car.jpg").into(url);

    buttonLoad = (Button) findViewById(R.id.buttonLoad);
    {
        buttonLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                url.setVisibility(View.VISIBLE);
            }
        });
    }

to

final ImageView url = (ImageView) findViewById(R.id.imageView);

    buttonLoad = (Button) findViewById(R.id.buttonLoad);
        buttonLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {             
Picasso.with(this).load("http://test.please.do/app/car.jpg").into(url);
            }
        });

and try to remove the code to make it invisible 并尝试删除代码以使其不可见

If you use View.INVISIBLE , the view still take the space even if it is shown as transparent. 如果使用View.INVISIBLE ,即使视图显示为透明,该视图仍会占用该空间。 If you want to totally hide the ImageView, you should use View.GONE . 如果要完全隐藏ImageView,则应使用View.GONE

The difference is simply that View.GONE collapse the view so the space is not left empty for her. 区别仅在于View.GONE折叠视图,因此该空间不会留给她。

From the two links above: 从上面的两个链接中:

View.INVISIBLE 查看不可见

This view is invisible, but it still takes up space for layout purposes. 该视图是不可见的,但仍会占用空间以进行布局。

View.GONE 视图消失

This view is invisible, and it doesn't take any space for layout purposes. 该视图是不可见的,并且它不占用任何空间用于布局目的。 Se the two links for more informations Se两个链接以获取更多信息

Edit 编辑

Another possible way could be adding by code the imageView after the button click.. Or another way could be setting height of the imageView to 0dp and then back to WRAP_CONTENT . 另一种可能的方法是在单击按钮后通过代码添加0dp 。或者另一种方法是将0dp高度设置为0dp然后返回WRAP_CONTENT

Those are possibilities, you can do it . 这些都是可能的,您可以做到 But I highly reccoment using View.GONE and View.VISIBLE for handling stuffs like this since it is the "Standard" for View behaviours 但是我强烈建议使用View.GONEView.VISIBLE处理此类内容,因为它是View行为的“标准”

You should add the image button after the old layout. 您应该在旧版式之后添加图片按钮。 like this: 像这样:

<RelativeLayout ...>
    <variable..../>
    <ImageButton..../>
</RelativeLayout>

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

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