安卓view,imageview动态加载图片

安卓view,imageview动态加载图片

 

安卓网络权限设置


    <uses-permission android:name="android.permission.INTERNET" />

依赖

// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
    implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.1'

布局设置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight ="1"
        tools:context=".MainActivity">

    <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight ="1"
            android:id="@+id/button" tools:ignore="MissingConstraints"
            android:background="@mipmap/ic_launcher"
    />

    <ImageButton
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight ="1"
            tools:srcCompat="@tools:sample/avatars"
            android:id="@+id/imageButton"
            tools:ignore="MissingConstraints"/>


</LinearLayout >

代码

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {



    private Button btn;
    private ImageView img;


    //在消息队列中实现对控件的更改
    @SuppressLint("HandlerLeak")
    private Handler handle = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        public void handleMessage(final Message msg) {
            switch (msg.what) {
                case 0:
                    System.out.println("111");
                    final Bitmap bmp=(Bitmap)msg.obj;
                    img.setImageBitmap(bmp);


                    Drawable drawable = new Drawable() {
                        @Override
                        public void draw(Canvas canvas) {

                            View btn = MainActivity.this.btn;

                            //这里的图片拉伸还需要研究一下
                            canvas.drawBitmap(bmp,  btn.getMatrix(), null);
                        }

                        @Override
                        public void setAlpha(int i) {

                        }

                        @Override
                        public void setColorFilter(ColorFilter colorFilter) {

                        }

                        @Override
                        public int getOpacity() {
                            return 0;
                        }
                    };
                    btn.setBackground(drawable);

                    break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.button);
        img = (ImageView) findViewById(R.id.imageButton);

        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Bitmap bmp = getURLimage(null);
                Message msg = new Message();
                msg.what = 0;
                msg.obj = bmp;
                handle.sendMessage(msg);
            }
        }).start();
    }

    //加载图片
    public Bitmap getURLimage(String url) {
        Bitmap bmp = null;
        try {

            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            final Request request = new Request.Builder()
                    .url("https://up.enterdesk.com/edpic/4c/a6/31/4ca631a8841304be2351295d50cf801d.jpg")
                    .method("GET", null)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response execute = client.newCall(request).execute();
            InputStream inputStream = execute.body().byteStream();

            bmp = BitmapFactory.decodeStream(inputStream);
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值