android Glide CustomViewTarget,java

android Glide CustomViewTarget,java

 

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.transition.Transition;

public class MainActivity extends AppCompatActivity {

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

        MyView myView = new MyView(this, null);
        setContentView(myView);

        CustomViewTarget target = new CustomViewTarget(myView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) {
                ImageView iv = myView.findViewById(R.id.image);
                iv.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }
        };

        Glide.with(this).load(R.mipmap.ic_launcher).into(target);
    }
}

 

 

 

my_view.xml:


 

 

 

MyView.java:

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;


public class MyView extends LinearLayout {
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

        removeAllViews();

        View view = LayoutInflater.from(context).inflate(R.layout.my_view, null);
        addView(view);
    }
}

 

 

    implementation 'com.github.bumptech.glide:glide:4.15.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.15.0'

 

 

你可能感兴趣的:(Android,Java,Glide,android,glide,java)