Skip to content

Commit

Permalink
add leftImgVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
huiger committed Aug 17, 2017
1 parent 032439f commit debe618
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.huige.basetitlelayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.huige.library.BaseTitleLayout;
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<com.huige.library.BaseTitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleLeftText1="返回"
app:titleLeftText="返回"
android:background="#000"
app:titleTextColor="#fff"
app:titleCenterText="测试一下"/>
Expand All @@ -31,4 +31,11 @@
app:titleRightImg2="@mipmap/collect_icon"
app:titleCenterText="测试一下测试一下测试一下测试一下测试一下"/>

<com.huige.library.BaseTitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleCenterText="测试无LeftImg"
app:titleLeftImgVisible="GONE"
android:layout_marginTop="15dp"/>

</LinearLayout>
37 changes: 27 additions & 10 deletions library/src/main/java/com/huige/library/BaseTitleLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class BaseTitleLayout extends View {
private int rightImg1 = -1, rightImg2 = -1;
private String rightText = "";
private Bitmap leftBitmap;
private LeftImgVisible mLeftImgVisible = LeftImgVisible.VISIBLE;

private TitleLayoutClickListener listener;

Expand Down Expand Up @@ -73,7 +74,16 @@ public BaseTitleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
} else if (index == R.styleable.BaseTitleLayout_titleLeftImg) {
leftImg = typedArray.getResourceId(index, R.drawable.back);

} else if (index == R.styleable.BaseTitleLayout_titleLeftText1) {
} else if (index == R.styleable.BaseTitleLayout_titleLeftImgVisible) {
int style = typedArray.getInt(index, LeftImgVisible.VISIBLE.ordinal());
for (LeftImgVisible leftImgVisible : LeftImgVisible.values()) {
if(style == leftImgVisible.ordinal()) {
mLeftImgVisible = leftImgVisible;
}
}


} else if (index == R.styleable.BaseTitleLayout_titleLeftText) {
leftText = typedArray.getString(index);

} else if (index == R.styleable.BaseTitleLayout_titleCenterText) {
Expand Down Expand Up @@ -108,17 +118,19 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.mCanvas = canvas;

leftBitmap = BitmapFactory.decodeResource(getResources(), leftImg);
rect.top = layoutHeight / 2 - (iconSize + dp2px(5)) / 2;
rect.bottom = layoutHeight / 2 + (iconSize + dp2px(5)) / 2;
rect.left = paddingLeft;
rect.right = paddingLeft + iconSize;
mCanvas.drawBitmap(leftBitmap, null, rect, mPaint);
if(mLeftImgVisible == LeftImgVisible.VISIBLE) {
leftBitmap = BitmapFactory.decodeResource(getResources(), leftImg);
rect.top = layoutHeight / 2 - (iconSize + dp2px(5)) / 2;
rect.bottom = layoutHeight / 2 + (iconSize + dp2px(5)) / 2;
rect.left = paddingLeft;
rect.right = paddingLeft + iconSize;
mCanvas.drawBitmap(leftBitmap, null, rect, mPaint);
}


Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float textCenterHeight = getHeight() / 2 - fontMetrics.descent + (fontMetrics.descent - fontMetrics.ascent)/2;
float textCenterHeight = getHeight() / 2 - fontMetrics.descent + (fontMetrics.descent - fontMetrics.ascent) / 2;

if (!TextUtils.isEmpty(leftText)) {
mCanvas.drawText(leftText, paddingLeft + (iconSize + dp2px(5)), textCenterHeight, mPaint);
}
Expand Down Expand Up @@ -224,7 +236,7 @@ public boolean onTouchEvent(MotionEvent event) {
listener.onRightTextClickListener();
}
} else if (x < layoutWidth - iconSize - paddingRight && x > layoutWidth - 2 * iconSize - paddingRight) {
if(rightImg2 != -1) {
if (rightImg2 != -1) {
listener.onRightImg2ClickListener();
}
}
Expand All @@ -236,4 +248,9 @@ private int dp2px(float dp) {
float scale = getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}

private enum LeftImgVisible{
GONE,
VISIBLE
}
}
6 changes: 5 additions & 1 deletion library/src/main/res/values/arrts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
<attr name="titleTextSize" format="dimension"/>
<attr name="titleIconSize" format="dimension"/>
<attr name="titleLeftImg" format="reference"/>
<attr name="titleLeftText1" format="string"/>
<attr name="titleLeftImgVisible" format="enum">
<enum name="VISIBLE" value="1"/>
<enum name="GONE" value="0"/>
</attr>
<attr name="titleLeftText" format="string"/>
<attr name="titleCenterText" format="string"/>
<attr name="titleRightImg1" format="reference"/>
<attr name="titleRightImg2" format="reference"/>
Expand Down

0 comments on commit debe618

Please sign in to comment.