Skip to content

Commit

Permalink
【Jiang】:添加一个ItemView控件
Browse files Browse the repository at this point in the history
  • Loading branch information
jxr202 committed Mar 30, 2018
1 parent 8117fba commit 1901649
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 29 deletions.
7 changes: 3 additions & 4 deletions colorful-ui-example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
compileSdkVersion 26

defaultConfig {
applicationId "com.jxr202.colorful_ui_example"
minSdkVersion 18
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"

Expand All @@ -27,7 +26,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(':colorful-ui')
Expand Down
17 changes: 17 additions & 0 deletions colorful-ui-example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
android:background="#B6D989"
jxr202:leftDrawable="@mipmap/ic_common_back"
jxr202:leftImageVisibility="visible"
jxr202:rightDrawable="@mipmap/ic_common_share"
jxr202:rightImageVisibility="visible"
jxr202:titleColor="#030303"
jxr202:titleText="@string/app_name"
jxr202:titleTextSize="15sp"
Expand Down Expand Up @@ -45,4 +47,19 @@
android:textSize="16sp"
android:text="@string/date_title"/>

<com.jxr202.colorful_ui.ItemView
android:id="@+id/itemView"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#ffffff"
jxr202:leftImage="@mipmap/we_chat_sport_logo"
jxr202:leftTitleText="微信运动"
jxr202:leftTitleTextSize="15sp"
jxr202:leftTitleTextColor="#010101"
jxr202:rightText="24小时不停"
jxr202:rightTextSize="14sp"
jxr202:rightTextColor="#999999"
jxr202:rightImage="@drawable/ic_item_more"
jxr202:bottomLineColor="#cccccc"/>

</LinearLayout >
7 changes: 3 additions & 4 deletions colorful-ui/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
compileSdkVersion 26

defaultConfig {
minSdkVersion 18
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"

Expand All @@ -26,6 +25,6 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:appcompat-v7:26.1.0'
testCompile 'junit:junit:4.12'
}
12 changes: 12 additions & 0 deletions colorful-ui/src/main/java/com/jxr202/colorful_ui/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@

public class CommonUtils {

private static long lastClickTime;

public static boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
if (0 < timeD && timeD < 800) {
return true;
}
lastClickTime = time;
return false;
}

public static float dp2px(Resources resources, float dp) {
final float scale = resources.getDisplayMetrics().density;
return dp * scale + 0.5f;
Expand Down
141 changes: 141 additions & 0 deletions colorful-ui/src/main/java/com/jxr202/colorful_ui/ItemView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.jxr202.colorful_ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
* Created by Jxr35 on 2018/3/29
*/

public class ItemView extends RelativeLayout {

private ImageView mLeftIcon;
private ImageView mRightIcon;
private TextView mLeftTitle;
private TextView mRightSummary;
private View mBottomLine;

private Context mContext;
private int mLeftIconId;
private int mRightIconId;

public ItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initTypedArray(context, attrs, 0);
}

public ItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initTypedArray(context, attrs, defStyleAttr);
}

private void initTypedArray(Context context, AttributeSet attrs, int defStyleAttr) {
mContext = context;
mLeftIcon = new ImageView(context);
mLeftTitle = new TextView(context);
mRightIcon = new ImageView(context);
mRightSummary = new TextView(context);
mBottomLine = new View(context);

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ItemView, defStyleAttr, 0);

mLeftIconId = array.getResourceId(R.styleable.ItemView_leftImageId, R.id.leftIconId);
mLeftIcon.setId(mLeftIconId);
mLeftIcon.setImageDrawable(array.getDrawable(R.styleable.ItemView_leftImage));
mLeftIcon.setVisibility(array.getInteger(R.styleable.ItemView_leftImageVisibility, VISIBLE));

mLeftTitle.setId(array.getResourceId(R.styleable.ItemView_leftTitleId, R.id.leftTextId));
mLeftTitle.setText(array.getString(R.styleable.ItemView_leftTitleText));
mLeftTitle.setTextSize(px2dip(array.getDimension(R.styleable.ItemView_leftTitleTextSize, 15)));
mLeftTitle.setTextColor(array.getColor(R.styleable.ItemView_leftTitleTextColor, 0xff010101));
mLeftTitle.setVisibility(array.getInteger(R.styleable.ItemView_leftTitleTextVisibility, VISIBLE));
mLeftTitle.setEllipsize(TextUtils.TruncateAt.END);
mLeftTitle.setLines(1);
mLeftTitle.setPadding(dp2px(15), 0, dp2px(15), 0);

mRightIconId = array.getResourceId(R.styleable.ItemView_rightImageId, R.id.rightIconId);
mRightIcon.setId(mRightIconId);
mRightIcon.setImageDrawable(array.getDrawable(R.styleable.ItemView_rightImage));
mRightIcon.setVisibility(array.getInteger(R.styleable.ItemView_rightImageVisibility, VISIBLE));

mRightSummary.setId(array.getResourceId(R.styleable.ItemView_rightTextId, R.id.leftTextId));
mRightSummary.setText(array.getString(R.styleable.ItemView_rightText));
mRightSummary.setTextSize(px2dip(array.getDimension(R.styleable.ItemView_rightTextSize, 15)));
mRightSummary.setTextColor(array.getColor(R.styleable.ItemView_rightTextColor, 0xff999999));
mRightSummary.setVisibility(array.getInteger(R.styleable.ItemView_rightTextVisibility, VISIBLE));
mRightSummary.setGravity(Gravity.END | Gravity.CENTER);

mBottomLine.setBackgroundColor(array.getColor(R.styleable.ItemView_bottomLineColor, 0xffff00ff));

array.recycle();
initView();
}

private void initView() {
RelativeLayout.LayoutParams leftIconParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams leftTitleParams = new RelativeLayout.LayoutParams(dp2px(180), ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rightIconParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rightTextParams = new RelativeLayout.LayoutParams(dp2px(80), ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutParams bottomLineParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);

leftIconParams.addRule(CENTER_VERTICAL);
leftIconParams.setMarginStart(dp2px(10));

leftTitleParams.addRule(CENTER_VERTICAL);
leftTitleParams.addRule(END_OF, mLeftIconId);
leftTitleParams.setMarginStart(dp2px(5));
leftTitleParams.setMarginEnd(dp2px(5));

rightIconParams.addRule(CENTER_VERTICAL);
rightIconParams.addRule(ALIGN_PARENT_END);
rightIconParams.setMarginEnd(dp2px(10));

rightTextParams.addRule(CENTER_VERTICAL);
rightTextParams.addRule(START_OF, mRightIconId);

bottomLineParams.addRule(ALIGN_PARENT_BOTTOM);

addView(mLeftIcon, leftIconParams);
addView(mLeftTitle, leftTitleParams);
addView(mRightSummary, rightTextParams);
addView(mRightIcon, rightIconParams);
addView(mBottomLine, bottomLineParams);
}

public void setLeftIcon(Drawable drawable) {
mLeftIcon.setImageDrawable(drawable);
}

public void setTitleText(String text) {
mLeftTitle.setText(text);
}

public void setRightText(String text) {
mRightSummary.setText(text);
}

private int px2dip(float pxValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

/**
* convert dp to its equivalent px
* <p>
* 将dp转换为与之相等的px
*/
public int dp2px(float dipValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 71 additions & 21 deletions colorful-ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<resources >

<attr name="leftImage" format="color|reference"/>
<attr name="leftImageId" format="reference|integer"/>
<attr name="leftImageVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>

<attr name="leftTitleId" format="reference|integer"/>
<attr name="leftTitleText" format="string"/>
<attr name="leftTitleTextSize" format="dimension"/>
<attr name="leftTitleTextColor" format="color"/>
<attr name="leftTitleTextVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>

<attr name="rightTextId" format="reference|integer"/>
<attr name="rightText" format="string"/>
<attr name="rightTextSize" format="dimension"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightTextVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>

<attr name="rightImage" format="color|reference"/>
<attr name="rightImageId" format="reference|integer"/>
<attr name="rightImageVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>

<declare-styleable name="dateStyle">
<attr name="uiBackground" format="color|reference"/>

Expand Down Expand Up @@ -44,12 +80,8 @@
<!-- ****************标题栏*******************-->
<declare-styleable name="titleBar">
<attr name="leftDrawable" format="color|reference"/>
<attr name="leftImageId" format="reference|integer"/>
<attr name="leftImageVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>
<attr name="leftImageId"/>
<attr name="leftImageVisibility"/>

<attr name="titleText" format="string"/>
<attr name="titleTextSize" format="dimension"/>
Expand All @@ -60,23 +92,41 @@
<enum name="visible" value="0" />
</attr>

<attr name="rightTextId" format="reference|integer"/>
<attr name="rightText" format="string"/>
<attr name="rightTextSize" format="dimension"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightTextVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>
<attr name="rightTextId"/>
<attr name="rightText"/>
<attr name="rightTextSize"/>
<attr name="rightTextColor"/>
<attr name="rightTextVisibility"/>

<attr name="rightDrawable" format="color|reference"/>
<attr name="rightImageId" format="reference|integer"/>
<attr name="rightImageVisibility">
<enum name="gone" value="8" />
<enum name="invisible" value="4" />
<enum name="visible" value="0" />
</attr>
<attr name="rightImageId"/>
<attr name="rightImageVisibility"/>
</declare-styleable>

<!-- **************** ItemView *******************-->
<declare-styleable name="ItemView">
<attr name="leftImage"/>
<attr name="leftImageId"/>
<attr name="leftImageVisibility"/>

<attr name="leftTitleId"/>
<attr name="leftTitleText"/>
<attr name="leftTitleTextSize"/>
<attr name="leftTitleTextColor"/>
<attr name="leftTitleTextVisibility"/>

<attr name="rightTextId" />
<attr name="rightText"/>
<attr name="rightTextSize"/>
<attr name="rightTextColor"/>
<attr name="rightTextVisibility"/>

<attr name="rightImage"/>
<attr name="rightImageId"/>
<attr name="rightImageVisibility"/>

<attr name="bottomLineColor" format="color"/>
</declare-styleable>


</resources >
3 changes: 3 additions & 0 deletions colorful-ui/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="date_title" type="id"/>
<item name="leftIconId" type="id"/>
<item name="leftTextId" type="id"/>
<item name="rightIconId" type="id"/>
</resources>

0 comments on commit 1901649

Please sign in to comment.