TextAndImgShowView.java 2.82 KB
package com.shunzhi.parent.views;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.share.mvpsdk.utils.DisplayUtils;
import com.shunzhi.parent.AppConfig;
import com.shunzhi.parent.R;


/**
 * Created by ToaHanDong on 2018/1/22.
 */

public class TextAndImgShowView extends LinearLayout {
    public TextAndImgShowView(Context context) {
        super(context);
        init(context);
    }

    public TextAndImgShowView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TextAndImgShowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    ImageView image;
    TextView text;
    FrameLayout layout;
    int[] imgs = null;
    boolean isSelect = false;

    private void init(Context context) {
        View view = View.inflate(context, R.layout.text_and_img_show, this);
        image = view.findViewById(R.id.image);
        text = view.findViewById(R.id.text);
        layout = view.findViewById(R.id.layout);
    }

    public void setImgs(@DrawableRes int normalDrawable, @DrawableRes int selectDrawable) {
        imgs = new int[]{normalDrawable, selectDrawable};
    }

    public void addImgs(String imgUrl){
//        Log.d("66666","imgUrl="+imgUrl);
        if (!imgUrl.startsWith("http"))imgUrl= AppConfig.BASE_URL_FILE+imgUrl;
        Glide.with(getContext()).load(imgUrl).error(R.drawable.gxzt).into(image);
    }

    public void setSelect(boolean isSelect) {
        if (isSelect) {
            if (imgs != null && imgs.length > 1)
                image.setImageResource(imgs[1]);
        } else {
            if (imgs != null && imgs.length > 1)
                image.setImageResource(imgs[0]);
        }
    }


    public void setText(String content) {
        text.setText(content);
    }

    public void setTextColor(int color) {
        text.setTextColor(color);
    }

    public void setWidth(Activity activity, View layout_control) {
//        Log.d("66666", "layout_control=" + layout_control.getMeasuredWidth());
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        params.width = (DisplayUtils.getScreenWidthPixels(activity) -
                DisplayUtils.dp2px(layout_control.getPaddingLeft() + layout_control.getPaddingRight()
        )) / 4;
        layout.setLayoutParams(params);
        LayoutParams params1 = new LayoutParams(52,52);
        image.setLayoutParams(params1);
    }
}