短视频系统源代码,加载本地图片和加载网络图片

在短视频系统源代码中,上传照片时会涉及到加载本地图片或加载网络图片的情况,下面介绍一下相关代码
从布局开始



 
    
        
 
    
 
    
 
        {
     item.title}" />
 
        {
     item.label}" />
 
        {
     item.content}"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
 
        {
     item.image}" />
 
        {
     item.photo}" />
 
    

里面有文字有图片

然后创建一个Item

public class Item {
     
    private String title;
    private String label;
    private String content;
    private String image;
    private int photo;
 
 
    public String getImage() {
     
        return image;
    }
 
    public void setImage(String image) {
     
        this.image = image;
    }
 
    public Item(String title, String label , String content, String image,int photo) {
     
        this.title = title;
        this.label = label;
        this.content = content;
        this.image = image;
        this.photo = photo;
    }
 
    public int getPhoto() {
     
        return photo;
    }
 
    public void setPhoto(int photo) {
     
        this.photo = photo;
    }
 
    public String getTitle() {
     
        return title;
    }
 
    public void setTitle(String title) {
     
        this.title = title;
    }
 
    public String getLabel() {
     
        return label;
    }
 
    public void setLabel(String label) {
     
        this.label = label;
    }
 
    public String getContent() {
     
        return content;
    }
 
    public void setContent(String content) {
     
        this.content = content;
    }
}

代码中三个步骤

public class DataActivity extends AppCompatActivity{
     
 
    DataActivityLayoutBinding binding;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        // 1.获取标签对象
         binding = DataBindingUtil.setContentView(this,R.layout.data_activity_layout);
        // 2.创建Item对象
        Item item = new Item("父亲的背景","散文","我与父亲不相见已二年余了,我最不能忘记的是他的背影", "https://img.52z.com/upload/news/image/20180621/20180621055651_47663.jpg",R.mipmap.ele);
        // 3.绑定到mUser到布局对象中
        binding.setItem(item);

里面有一个网址和一个本地图片, 如果这样运行是不会显示图片的,还要有BindingAdapter 注解才行

具体代码如下

public class ImageViewAdapter {
     
    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, Bitmap bitmap) {
     
        view.setImageBitmap(bitmap);
    }
 
    @BindingAdapter("android:src")
    public static void setSrc(ImageView view, int resId) {
     
        view.setImageResource(resId);
    }
 
 
    @BindingAdapter("imageUrl")
    public static void setSrc(ImageView imageView, String url) {
     
        Glide.with(imageView.getContext()).load(url)
                .placeholder(R.mipmap.ic_launcher)
                .into(imageView);
    }
 
 
    @BindingAdapter({
     "app:imageUrl", "app:placeHolder", "app:error"})
    public static void loadImage(ImageView imageView, String url, Drawable holderDrawable, Drawable errorDrawable) {
     
        Glide.with(imageView.getContext())
                .load(url)
                .placeholder(holderDrawable)
                .error(errorDrawable)
                .into(imageView);
    }
 
 
}

以上就是短视频系统源代码中关于加载本地图片和加载网络图片的相关代码,更多信息欢迎关注之后的文章
本文转载自网络,转载仅为分享干货知识,如有侵权欢迎联系云豹科技进行删除处理

你可能感兴趣的:(技术类,网络,android,python,移动开发,ios)