Skip to content

Commit

Permalink
增加multi_drawable
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoqi committed May 8, 2019
1 parent f859f68 commit 72b5f59
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 36 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.

36 changes: 36 additions & 0 deletions BackgroundLibrary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,42 @@
</context>
</template>
<template name="bl_unExpanded_textColor" value="app:bl_unExpanded_textColor=&quot;$value$&quot;" description="unExpanded文字颜色" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_multi_selector1" value="app:bl_multi_selector1=&quot;$value$&quot;" description="selector一条属性多状态表示" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_multi_selector2" value="app:bl_multi_selector1=&quot;$value$&quot;" description="selector一条属性多状态表示" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_multi_selector3" value="app:bl_multi_selector1=&quot;$value$&quot;" description="selector一条属性多状态表示" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_multi_selector4" value="app:bl_multi_selector1=&quot;$value$&quot;" description="selector一条属性多状态表示" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_multi_selector5" value="app:bl_multi_selector1=&quot;$value$&quot;" description="selector一条属性多状态表示" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
</context>
</template>
<template name="bl_multi_selector6" value="app:bl_multi_selector1=&quot;$value$&quot;" description="selector一条属性多状态表示" toReformat="true" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="XML" value="true" />
Expand Down
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ A framework for directly generating shape through Tags, no need to write shape.x
1.4.2 增加属性bl_activated_textColor, bl_active_textColor, bl_expanded_textColor, bl_unActivated_textColor, bl_unActive_textColor, bl_unExpanded_textColor以及BLConstraintLayout
1.4.3 1.4.3开始自动加入混淆配置,无需自己配置
1.4.4 修复与kotlin直接通过id获取view冲突的问题,并且可以在代码内直接使用BLView
1.4.5 新增属性bl_multi_selector1~bl_multi_selector6支持selector的一条属性多个状态的设置


## 示例效果
Expand Down Expand Up @@ -240,6 +241,18 @@ A framework for directly generating shape through Tags, no need to write shape.x
.setStrokeWidth(dip2px(1))
.build();


### bl_multi_selector属性
| 名称 | 类型 |备注|
|---|---|---|
|bl_multi_selector1| String| 支持selector的一条属性设置同时设置多个状态的写法(#jump2),内容规则为以",
"为分隔符,最后一项为drawable资源id的名字,可选状态为state_checkable,state_checked,state_enabled,state_selected,state_pressed,state_focused,state_hovered
,state_activated,默认为true,如果为false则在前面加上"-"即可,例如-state_checkable,具体可以查看使用例子8|
|bl_multi_selector2| String| 同上|
|bl_multi_selector3| String| 同上|
|bl_multi_selector4| String| 同上|
|bl_multi_selector5| String| 同上|
|bl_multi_selector6| String| 同上|

## 使用例子

Expand Down Expand Up @@ -460,6 +473,48 @@ style中不要加入"app:", 直接写属性名即可
app:bl_frame_drawable_item13="@drawable/img13"
app:bl_frame_drawable_item14="@drawable/img14"/>

8.<span id="jump2">bl_multi_selector属性用法</span>

下例中的test_sel1,item中同时设置了android:state_pressed="true" android:state_focused="true"


<EditText
android:layout_width="300dp"
android:layout_height="36dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:clickable="true"
android:text="一条属性多个状态(原生写法)"
android:textColor="@android:color/black"
android:textSize="18dp"
android:textStyle="bold"
android:background="@drawable/test_sel1"/>
test_sel1:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_focused="true"
android:drawable="@drawable/test_bg_sel" /> <!-- pressed -->
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@drawable/test_bg_sel2" /> <!-- focused -->
</selector>
bl_multi_selector:用法,最多支持6条

<EditText
android:padding="0dp"
android:layout_width="300dp"
android:layout_height="36dp"
android:layout_marginTop="15dp"
android:clickable="true"
android:gravity="center"
android:text="一条属性多个状态(原生写法)"
android:textColor="@android:color/black"
android:textSize="18dp"
android:textStyle="bold"
app:bl_multi_selector1="state_pressed,state_focused,test_bg_sel"
app:bl_multi_selector2="-state_pressed,-state_focused,test_bg_sel2"/>



## 简单的性能测试
我用原生的方法写了例子里面的shape和selector,做了一个比较简单的测试:

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.noober.background:core:1.4.4'
// implementation project(':library')
// implementation 'com.noober.background:cbackground_libraryore:1.4.4'
implementation project(':library')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/test_bg_sel.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp"/>
<solid android:color="#7CCD7C"/>
<stroke android:color="#F0F0F0" android:width="2dp"/>
</shape>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/test_bg_sel2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp"/>
<solid android:color="#7CFC00"/>
<stroke android:color="#7CFC00" android:width="2dp"/>
</shape>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/test_sel1.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
<item android:state_pressed="true" android:state_focused="true"
android:drawable="@drawable/test_bg_sel" /> <!-- pressed -->
<item android:state_pressed="false"
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@drawable/test_bg_sel2" /> <!-- focused -->
</selector>
23 changes: 19 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,31 @@
app:bl_stroke_width="1dp"
app:bl_unPressed_drawable="#00FFFFFF" />

<TextView
android:layout_width="180dp"
<EditText
android:layout_width="300dp"
android:layout_height="36dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:text="COBB ANGLE"
android:clickable="true"
android:text="一条属性多个状态(原生写法)"
android:textColor="@android:color/black"
android:textSize="18dp"
android:textStyle="bold"
android:background="@drawable/test_sel1"/>

<com.noober.background.view.BLEditText
android:padding="0dp"
android:layout_width="300dp"
android:layout_height="36dp"
android:layout_marginTop="15dp"
android:clickable="true"
android:gravity="center"
android:text="一条属性多个状态(原生写法)"
android:textColor="@android:color/black"
android:textSize="18dp"
android:textStyle="bold"
android:background="@drawable/shape1"/>
app:bl_multi_selector1="state_pressed,state_focused,test_bg_sel"
app:bl_multi_selector2="-state_pressed,-state_focused,test_bg_sel2"/>

<TextView
android:layout_width="180dp"
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
classpath 'com.novoda:bintray-release:0.8.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
}

allprojects {
Expand All @@ -35,7 +35,7 @@ ext {
userOrg = 'noober'
groupId = 'com.noober.background'
uploadName = 'LibraryForBackground'
publishVersion = '1.4.4'
publishVersion = '1.4.5'
desc = "A framework for directly generating shape through Tags, no need to write shape.xml again(通过标签直接生成shape,无需再写shape.xml)"
website = 'https://github.com/JavaNoober/BackgroundLibrary'
// gradlew clean build bintrayUpload -PbintrayUser=xiaoqiandroid -PbintrayKey=xxxxxxxxxxxxxxxx -PdryRun=false
Expand Down
29 changes: 14 additions & 15 deletions library/src/main/java/com/noober/background/BackgroundFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,13 @@ private static View setViewBackground(String name, Context context, AttributeSet
setDrawable(drawable, view, otherTa);
} else if(animTa.getIndexCount() > 0){
AnimationDrawable animationDrawable = DrawableFactory.getAnimationDrawable(animTa);
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
view.setBackground(animationDrawable);
}else {
view.setBackgroundDrawable(animationDrawable);
}
setBackground(animationDrawable, view);
if(animTa.getBoolean(R.styleable.bl_anim_bl_anim_auto_start, false)){
animationDrawable.start();
}
} else if(multiSelTa.getIndexCount() > 0){
stateListDrawable = DrawableFactory.getMultiSelectorDrawable(context, multiSelTa);
setBackground(stateListDrawable, view);
}

if (view instanceof TextView && textTa.getIndexCount() > 0) {
Expand Down Expand Up @@ -221,22 +220,22 @@ private static void setDrawable(Drawable drawable, View view, TypedArray otherTa
((TextView)view).setCompoundDrawables(null, null, null, drawable);
}
}else {
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
view.setBackground(drawable);
}else {
view.setBackgroundDrawable(drawable);
}
setBackground(drawable, view);
}
}else {
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
view.setBackground(drawable);
}else {
view.setBackgroundDrawable(drawable);
}
setBackground(drawable, view);
}

}

private static void setBackground(Drawable drawable, View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
}

public void setInterceptFactory(LayoutInflater.Factory factory) {
mViewCreateFactory = factory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
public enum MultiSelector {

State_Checkable("state_checkable", android.R.attr.state_checkable),
State_Checked("state_checkable", android.R.attr.state_checkable),
State_Enabled("state_checkable", android.R.attr.state_enabled),
State_Selected("state_checkable", android.R.attr.state_selected),
State_Pressed("state_checkable", android.R.attr.state_pressed),
State_Focused("state_checkable", android.R.attr.state_focused),
State_Hovered("state_checkable", android.R.attr.state_hovered),
State_Activated("state_checkable", android.R.attr.state_activated);
State_Checked("state_checked", android.R.attr.state_checked),
State_Enabled("state_enabled", android.R.attr.state_enabled),
State_Selected("state_selected", android.R.attr.state_selected),
State_Pressed("state_pressed", android.R.attr.state_pressed),
State_Focused("state_focused", android.R.attr.state_focused),
State_Hovered("state_hovered", android.R.attr.state_hovered),
State_Activated("state_activated", android.R.attr.state_activated);

public String value;
public int id;
Expand All @@ -18,4 +18,27 @@ public enum MultiSelector {
this.value = value;
this.id = id;
}

public static MultiSelector getMultiAttr(String value){
switch (value){
case "state_checkable":
return State_Checkable;
case "state_checked":
return State_Checked;
case "state_enabled":
return State_Enabled;
case "state_selected":
return State_Selected;
case "state_pressed":
return State_Pressed;
case "state_focused":
return State_Focused;
case "state_hovered":
return State_Hovered;
case "state_activated":
return State_Activated;
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class ResourceUtils {

@Nullable
public Drawable getDrawable(Context context, String resName) {
public static Drawable getDrawable(Context context, String resName) {
Resources resources = context.getResources();
int id = resources.getIdentifier(resName, "drawable", context.getPackageName());
if(id == 0){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.noober.background.drawable;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
Expand Down Expand Up @@ -43,4 +44,10 @@ public static StateListDrawable getPressDrawable(GradientDrawable drawable, Type
public static AnimationDrawable getAnimationDrawable(TypedArray animTa) throws Exception {
return (AnimationDrawable) new AnimationDrawableCreator(animTa).create();
}

public static StateListDrawable getMultiSelectorDrawable(Context context, TypedArray selectorTa)
throws Exception {
return (StateListDrawable) new MultiSelectorDrawableCreator(context, selectorTa).create();
}

}
Loading

0 comments on commit 72b5f59

Please sign in to comment.