单击按钮加载图像

时间:2017-09-08 14:36:52

标签: java android url button imageview

我有这个按钮,可以从URL加载图像。我尝试将ImageView可见性设置为Invisible,因此当我启动应用程序时,图像不会显示,直到我单击按钮。但我想知道,还有其他方法吗?任何其他方式使其工作而不将ImageView的可见性设置为不可见

这是我的代码:

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);
                }
            });
        }
    }

3 个答案:

答案 0 :(得分:0)

更改

<script>
    var myCenter = new google.maps.LatLng(51.059955, -114.211807);

    function initialize() {
        var mapProp = {
            center: myCenter,
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
        var marker = new google.maps.Marker({
            position: myCenter,
            animation: google.maps.Animation.DROP,
            icon: 'http://www.fergusandbix.com/images/FB-pin.png'
        });
        marker.setMap(map);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    var myCenter2 = new google.maps.LatLng(50.9156772, -113.9640719);

    function initialize() {
        var mapProp2 = {
            center: myCenter2,
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP2
        };
        var map = new google.maps.Map(document.getElementById("googleMap2"), mapProp2);
        var marker = new google.maps.Marker({
            position: myCenter2,
            animation: google.maps.Animation.DROP,
            icon: 'http://www.fergusandbix.com/images/FB-pin.png'
        });
        marker.setMap(map);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

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);
            }
        });
    }

并尝试删除代码以使其不可见

答案 1 :(得分:0)

如果您使用View.INVISIBLE,即使视图显示为透明,视图仍会占用空间。如果要完全隐藏ImageView,则应使用View.GONE

不同之处仅在于View.GONE会折叠视图,因此空间不会留空。

从上面的两个链接:

<强> View.INVISIBLE

  

此视图不可见,但仍会占用布局空间。

<强> View.GONE

  

此视图不可见,并且不需要任何空间进行布局。   查看两个链接以获取更多信息

修改

另一种可能的方法是在按钮点击后通过代码添加imageView。或者另一种方法是将imageView的高度设置为0dp,然后再返回WRAP_CONTENT

这些是的可能性,你可以做到。但是我使用View.GONEView.VISIBLE高度重新处理这样的事情,因为它是查看行为的“标准”

答案 2 :(得分:0)

您应该在旧布局后添加图像按钮。 像这样:

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