Android 封装图片为左图片有文字

来源:互联网 发布:如何开网络棋牌室赚钱 编辑:程序博客网 时间:2024/04/29 20:52
package com.wuzhou.corner.widget.find;


import com.wuzhou.corner.R.color;
import com.wuzhou.corner.widget.SceenMannage;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class FindButton extends RelativeLayout {


private Button leftButton;
private TextView leftText;
/**
* leftLinearLayout显示
*/
public final static int LEFT_LINEAR_VISIBILITY = 1;
/**
* leftLinearLayout不显示
*/
public final static int LEFT_LINEAR_INVISIBILITY = 2;
/**
* leftButton显示并且leftText不显示
*/
public final static int LEFT_BUTTON_VISIBILITY = 3;
/**
* leftText显示并且leftButton不显示
*/
public final static int LEFT_TEXT_VISIBILITY = 4;


private RelativeLayout leftLinearLayout, leftLinearLayout_1;
private int STANDARD_HEIGTH = 1080;// 标准宽的像素
private int STANDARD_WITH = 1800;// 标准高的像素+


private SceenMannage sm;
TypedArray typeA;


public FindButton(Context context) {
this(context, null, 0);
// TODO Auto-generated constructor stub
sm = new SceenMannage(context);
}


public FindButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
sm = new SceenMannage(context);
// TODO Auto-generated constructor stub
}


public FindButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
leftLinearLayout = new RelativeLayout(getContext());
leftLinearLayout_1 = new RelativeLayout(getContext());


leftButton = new Button(context);
leftButton.setFocusable(false);
leftButton.setClickable(false);
leftText = new TextView(context);
sm = new SceenMannage(context);


// tf = Typeface.createFromAsset(context.getAssets(), "fonts/yhcy.otf");
// 左边区域
RelativeLayout.LayoutParams leftLinearLayoutParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
leftLinearLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


RelativeLayout.LayoutParams leftLinearLayoutParams_1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.FILL_PARENT);
leftLinearLayoutParams_1.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);


RelativeLayout.LayoutParams leftLayoutParams = new RelativeLayout.LayoutParams(setSceemManage(50), setSceemManage(50));
leftLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
leftButton.setId(1);
leftLinearLayout_1.addView(leftButton, leftLayoutParams);


RelativeLayout.LayoutParams leftTextLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftTextLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
leftTextLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1);
leftText.setId(2);
leftLinearLayout_1.addView(leftText, leftTextLayoutParams);
leftLinearLayout.addView(leftLinearLayout_1, leftLinearLayoutParams_1);
leftLinearLayout.setBackgroundColor(Color.TRANSPARENT);
this.addView(leftLinearLayout, leftLinearLayoutParams);


}


/**
* 屏幕适配

* @param currentWidth
* @return
*/
public int setSceemManage(int currentWidth) {
DisplayMetrics metrics = getContext().getResources()
.getDisplayMetrics();
int large = (metrics.widthPixels > metrics.heightPixels) ? metrics.widthPixels
: metrics.heightPixels;
return (int) ((large * 1f / STANDARD_WITH) * 1.5 * currentWidth);
}


/**
* 给左边的自定义按钮设置文字

* @param text
*            填充的文字
* @param textSize
*            给填充的文字设置字体大小
* @param textColor
*            给填充的文字设置字体颜色
*/
public void setLeftButtonTextMessage(CharSequence text, int textSize,
int textColor) {
leftText.setTextSize(sm.setTextSize(textSize));
leftText.setTextColor(textColor);
leftText.setText(text);
}


public String getleftButtonTextMessage() {
return leftText.getText().toString();
}

/**
* 给最左边的自定义按钮设置图片

* @param resid
*/
public void setLeftButtonBg(int resid) {
leftButton.setBackgroundResource(resid);
}

public Button getLeftButton() {
return leftButton;
}


public void setLeftButton(Button leftButton) {
this.leftButton = leftButton;
}


/**
* 设置左侧按钮是否显示 LEFT_LINEAR_VISIBILITY = 1; //leftLinearLayout显示
* LEFT_LINEAR_INVISIBILITY = 2; //leftLinearLayout不显示
* LEFT_BUTTON_VISIBILITY = 3; //leftButton显示并且leftText不显示
* LEFT_TEXT_VISIBILITY = 4; //leftText显示并且leftButton不显示

* @param isVisibility
*/
public void setLeftButtonVisibly(int isVisibility) {
if (isVisibility == LEFT_LINEAR_VISIBILITY) {
leftLinearLayout.setVisibility(View.VISIBLE);
} else if (isVisibility == LEFT_LINEAR_INVISIBILITY) {
leftLinearLayout.setVisibility(View.INVISIBLE);
} else if (isVisibility == LEFT_BUTTON_VISIBILITY) {
leftButton.setVisibility(View.VISIBLE);
leftText.setVisibility(View.INVISIBLE);
} else if (isVisibility == LEFT_TEXT_VISIBILITY) {
leftButton.setVisibility(View.GONE);
leftText.setVisibility(View.VISIBLE);
}
}


/**
* 给左边的按钮设置监听事件
* @param l
*/
public void setOnLeftButtonClickListener(OnClickListener l) {
leftButton.setOnClickListener(l);
leftText.setOnClickListener(l);
leftLinearLayout.setOnClickListener(l);
setButtonFocusChanged(leftButton);
}


/**
* 按下这个按钮进行的颜色过滤
*/
public final static float[] BT_SELECTED = new float[] { 1, 0, 0, 0, 100, 0,
1, 0, 0, 100, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };


/**
* 按钮恢复原状的颜色过滤
*/
public final static float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };


/**
* 按钮焦点改变
*/
public final static OnFocusChangeListener buttonOnFocusChangeListener = new OnFocusChangeListener() {


@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v != null) {
if (hasFocus) {
// v.getBackground().setColorFilter(new
// ColorMatrixColorFilter(BT_SELECTED));
v.getBackground().setColorFilter(color.all_rel_back_or,
PorterDuff.Mode.MULTIPLY);
v.setBackgroundDrawable(v.getBackground());
} else {
// v.getBackground().setColorFilter(new
// ColorMatrixColorFilter(BT_NOT_SELECTED));
v.getBackground().setColorFilter(Color.RED,
PorterDuff.Mode.MULTIPLY);
v.setBackgroundDrawable(v.getBackground());
}
}
}
};


/**
* 按钮触碰按下效果
*/
public final static OnTouchListener buttonOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v != null) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(color.all_rel_back_or, PorterDuff.Mode.MULTIPLY);
v.setBackgroundDrawable(v.getBackground());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
v.setBackgroundDrawable(v.getBackground());
}
}
return false;
}
};


/**
* 设置图片按钮获取焦点改变状态
* @param inImageButton
*/
public final static void setButtonFocusChanged(View inView) {
inView.setOnTouchListener(buttonOnTouchListener);
inView.setOnFocusChangeListener(buttonOnFocusChangeListener);
}

}


2.在Activity中

               FindButton find_great = (FindButton) mViewHolder.findViewById(R.id.find_great);  //获取控件的ID
find_great.setLeftButtonBg(R.drawable.faxian_zan);                                                      //  获取图片
find_great.setLeftButtonTextMessage("赞", 14, Color.parseColor("#949494"));       //获取图片右边的文字

                如果想让图片和文字只显示一个则调用

              setLeftButtonVisibly(int isVisibility)这个方法


0 0
原创粉丝点击