效果展示:
自定义Android标题栏TitleBar布局,右边的图片可实现动画效果。
代码展示:
1.MainActivity中的布局:
<com.example.administrator.gamehleper.widget.TitleBar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="48dp" />
2.TitleBar类的代码:
public class TitleBar extends RelativeLayout {
private TextView tvGoBack, tvTitle;
private ImageView ivRight;
private GoBackClickListener goBackListenter;
private RightImageViewClickListener rightListener;
public TitleBar(Context context) {
super(context);
}
public TitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
/**
* 将自定义控件和布局文件进行绑定
* 参数一:View中可以通过getContext方法获取上下文对象
* 参数二:布局文件id
* 参数三:放置该布局的容器,也就是本类
*/
View view = View.inflate(getContext(), R.layout.layout_titlebar, this);
tvGoBack = (TextView) view.findViewById(R.id.tv_goback);
tvTitle = (TextView) view.findViewById(R.id.tv_title);
ivRight = (ImageView) view.findViewById(R.id.iv_right);
tvGoBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goBackListenter.onClick(v);
}
});
ivRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rightListener.onClick(v);
}
});
}
/**
* 设置标题
*
* @param title
*/
public void setTitle(String title) {
tvTitle.setText(title);
}
/**
* 设置标题颜色
*
* @param color
*/
public void setTitleColor(int color) {
tvTitle.setTextColor(color);
}
/**
* 设置右方图片的资源文件
*
* @param resId
*/
public void setRightImageView(int resId) {
ivRight.setImageResource(resId);
}
public void setLeftImageView (int resId){
tvGoBack.setText("");
tvGoBack.setBackgroundResource(resId);
}
public void setTitleTextSize(int size){
tvTitle.setTextSize(size);
}
/**
* 显示右方的图片
*/
public void showRight() {
ivRight.setVisibility(VISIBLE);
}
/**
* 隐藏右方的图片
*/
public void hideRight() {
ivRight.setVisibility(GONE);
}
/**
* 返回按钮点击的接口
*/
public interface GoBackClickListener {
void onClick(View view);
}
/**
* 右方图片点击时的接口回调
*/
public interface RightImageViewClickListener {
void onClick(View view);
}
public void setGoBackListerner(GoBackClickListener listener) {
this.goBackListenter = listener;
}
public void setRightListener(RightImageViewClickListener listener) {
this.rightListener = listener;
}
}
3.layout_titlebar布局中的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#00f0ff"
android:layout_height="48dp">
<TextView
android:id="@+id/tv_goback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_centerVertical="true"
android:layout_margin="4dp"
android:text="返回" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_centerInParent="true"
android:textSize="16sp"
android:text="title"/>
<com.qianfeng.gamehelper.widget.ImageViewScale
android:id="@+id/iv_right"
android:layout_width="48dp"
android:layout_alignParentRight="true"
android:layout_height="48dp"
android:padding="6dp"/>
</RelativeLayout>
4.实现图片动画效果ImageViewScale类的代码:
public class ImageViewScale extends ImageView
{
private boolean isClick;
private ScaleAnimation animatorNormal, animatorSmaller;
private float scaleFactor = 0.85f;
public ImageViewScale(Context context)
{
super(context);
init();
}
public ImageViewScale(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public ImageViewScale(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init();
}
private void init()
{
animatorNormal = new ScaleAnimation(scaleFactor, 1f, scaleFactor, 1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
animatorNormal.setDuration(300);
animatorNormal.setFillAfter(true);
animatorSmaller = new ScaleAnimation(1f, scaleFactor, 1f, scaleFactor,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
animatorSmaller.setDuration(300);
animatorSmaller.setFillAfter(true);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
isClick = true;
startAnimation(animatorSmaller);
break;
case MotionEvent.ACTION_MOVE:
isOutSide(event);
break;
case MotionEvent.ACTION_UP:
if (isClick)
{
isClick = false;
startAnimation(animatorNormal);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
performClick();
}
}, 300);
}
break;
}
return true;
}
private void isOutSide(MotionEvent event)
{
float viewX = event.getX();
float viewY = event.getY();
if (viewX < 0 || viewY < 0 || viewX > getWidth() || viewY > getHeight())
{
if (isClick)
{
isClick = false;
startAnimation(animatorNormal);
}
}
else if (viewX > 0 || viewY > 0 || viewX < getWidth()
|| viewY < getHeight())
{
if (!isClick)
{
isClick = true;
startAnimation(animatorSmaller);
}
}
}
}
5.在MainActivity类中代码:
这个方法用来修改titleBar的值的代码
protected void init() {
titleBar.setLeftImageView(R.mipmap.ic_launcher);
titleBar.setRightImageView(R.mipmap.fmt_home_iconfont_guanyu);
titleBar.setTitle("72G游戏助手");
titleBar.setTitleTextSize(24);
}
这个方法用来监听点击事件
protected void initEvent() {
titleBar.setGoBackListerner(new TitleBar.GoBackClickListener() {
@Override
public void onClick(View view) {
}
});
titleBar.setRightListener(new TitleBar.RightImageViewClickListener() {
@Override
public void onClick(View view) {
}
});
}