Skip to content

Commit df93025

Browse files
committed
update to 1.5.8
1 parent 029d276 commit df93025

8 files changed

Lines changed: 97 additions & 11 deletions

File tree

BackgroundLibrary.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,4 +773,10 @@
773773
<option name="XML" value="true" />
774774
</context>
775775
</template>
776+
<template name="bl_function" value="app:bl_function=&quot;$value$&quot;" description="点击事件的方法名" toReformat="true" toShortenFQNames="true">
777+
<variable name="value" expression="" defaultValue="" alwaysStopAt="true" />
778+
<context>
779+
<option name="XML" value="true" />
780+
</context>
781+
</template>
776782
</templateSet>

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ A framework for directly generating shape through Tags, no need to write shape.x
1010
依赖方式:
1111

1212
implementation "com.android.support:appcompat-v7:$supportVersion"
13-
implementation 'com.noober.background:core:1.5.7'
13+
implementation 'com.noober.background:core:1.5.8'
1414

1515
如果项目使用了androidx:
1616

1717
implementation "androidx.appcompat:appcompat:$supportVersion"
18-
implementation 'com.noober.background:core:1.5.7'
18+
implementation 'com.noober.background:core:1.5.8'
1919

2020

2121
## 使用文档
@@ -266,6 +266,18 @@ style中不要加入"app:", 直接写属性名即可
266266
app:bl_frame_drawable_item13="@drawable/img13"
267267
app:bl_frame_drawable_item14="@drawable/img14"/>
268268

269+
9、甚至支持直接在xml中设置方法,暂时只支持无参方法,支持父类方法
270+
271+
<Button
272+
android:id="@+id/btn"
273+
android:layout_width="320dp"
274+
android:layout_height="36dp"
275+
android:text="通过bl_function属性跳转到列表"
276+
app:bl_function="finish"/>
277+
278+
加入bl_function属性即可,这样控件就增加了finish点击事件,很多时候返回键只是一个finish,我们仅需要加入该属性即可,
279+
当然使用场景还有很多。
280+
269281
如果有什么问题,方便大家交流,创建了一个qq群,群号887686934,欢迎大家加入
270282

271283
![](https://user-gold-cdn.xitu.io/2018/11/22/1673a789b58ca9a6?w=10&h=10&f=png&s=94884)

app/src/main/java/com/noober/backgroudlibrary/MainActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public int dip2px(float dipValue) {
119119
return (int)(dipValue * scale + 0.5F);
120120
}
121121

122-
@BLUsed
123122
private void jumpToList(){
124123
startActivity(new Intent(MainActivity.this, ListActivity.class));
125124
}

app/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,18 @@
183183

184184
<Button
185185
android:id="@+id/btn"
186-
android:layout_width="130dp"
186+
android:layout_width="320dp"
187187
android:layout_height="36dp"
188188
android:layout_marginTop="5dp"
189189
android:background="@null"
190+
android:clickable="true"
190191
android:gravity="center"
191192
android:padding="0dp"
192-
android:text="跳转到列表"
193+
android:text="通过bl_function属性跳转到列表"
193194
android:textColor="#4F94CD"
194195
android:textSize="20sp"
195196
app:bl_corners_radius="20dp"
196-
android:onClick="finish"
197-
android:clickable="true"
197+
app:bl_function="jumpToList"
198198
app:bl_gradient_angle="0"
199199
app:bl_gradient_endColor="#4F94CD"
200200
app:bl_gradient_startColor="#63B8FF"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ext {
3636
userOrg = 'noober'
3737
groupId = 'com.noober.background'
3838
uploadName = 'LibraryForBackground'
39-
publishVersion = '1.5.7'
39+
publishVersion = '1.5.8'
4040
desc = "A framework for directly generating shape through Tags, no need to write shape.xml again(通过标签直接生成shape,无需再写shape.xml)"
4141
website = 'https://github.com/JavaNoober/BackgroundLibrary'
4242
// gradlew clean build bintrayUpload -PbintrayUser=xiaoqiandroid -PbintrayKey=xxxxxxxxxxxxxxxx -PdryRun=false

library/src/main/java/com/noober/background/BackgroundFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.lang.reflect.InvocationTargetException;
2929
import java.lang.reflect.Method;
3030
import java.util.HashMap;
31-
import java.util.List;
3231
import java.util.Map;
3332

3433
public class BackgroundFactory implements LayoutInflater.Factory2 {
@@ -39,7 +38,6 @@ public class BackgroundFactory implements LayoutInflater.Factory2 {
3938
private static final Class<?>[] sConstructorSignature = new Class[]{Context.class, AttributeSet.class};
4039
private static final Object[] mConstructorArgs = new Object[2];
4140
private static final Map<String, Constructor<? extends View>> sConstructorMap = new ArrayMap<>();
42-
4341
private static final HashMap<String, HashMap<String, Method>> methodMap = new HashMap<>();
4442

4543
@Override
@@ -203,7 +201,7 @@ private static View setViewBackground(String name, Context context, AttributeSet
203201

204202
if (otherTa.hasValue(R.styleable.bl_other_bl_function)) {
205203
String methodName = otherTa.getString(R.styleable.bl_other_bl_function);
206-
if (methodName != null) {
204+
if (!TextUtils.isEmpty(methodName)) {
207205
final Context currentContext = view.getContext();
208206
final Class parentClass = currentContext.getClass();
209207
final Method method = getMethod(parentClass, methodName);

libraryx/src/main/java/com/noober/background/BackgroundFactory.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import com.noober.background.view.Const;
2727

2828
import java.lang.reflect.Constructor;
29+
import java.lang.reflect.InvocationTargetException;
30+
import java.lang.reflect.Method;
31+
import java.util.HashMap;
2932
import java.util.Map;
3033

3134
public class BackgroundFactory implements LayoutInflater.Factory2 {
@@ -36,6 +39,7 @@ public class BackgroundFactory implements LayoutInflater.Factory2 {
3639
private static final Class<?>[] sConstructorSignature = new Class[]{Context.class, AttributeSet.class};
3740
private static final Object[] mConstructorArgs = new Object[2];
3841
private static final Map<String, Constructor<? extends View>> sConstructorMap = new ArrayMap<>();
42+
private static final HashMap<String, HashMap<String, Method>> methodMap = new HashMap<>();
3943

4044
@Override
4145
public View onCreateView(String name, Context context, AttributeSet attrs) {
@@ -195,6 +199,27 @@ private static View setViewBackground(String name, Context context, AttributeSet
195199
}
196200
}
197201

202+
if (otherTa.hasValue(R.styleable.bl_other_bl_function)) {
203+
String methodName = otherTa.getString(R.styleable.bl_other_bl_function);
204+
if (!TextUtils.isEmpty(methodName)) {
205+
final Context currentContext = view.getContext();
206+
final Class parentClass = currentContext.getClass();
207+
final Method method = getMethod(parentClass, methodName);
208+
view.setOnClickListener(new View.OnClickListener() {
209+
@Override
210+
public void onClick(View view) {
211+
try {
212+
method.invoke(currentContext);
213+
} catch (IllegalAccessException e) {
214+
e.printStackTrace();
215+
} catch (InvocationTargetException e) {
216+
e.printStackTrace();
217+
}
218+
}
219+
});
220+
}
221+
}
222+
198223
return view;
199224
} catch (Exception e) {
200225
e.printStackTrace();
@@ -212,6 +237,50 @@ private static View setViewBackground(String name, Context context, AttributeSet
212237
}
213238
}
214239

240+
241+
private static Method getMethod(Class clazz, String methodName) {
242+
Method method = null;
243+
HashMap<String, Method> methodHashMap = methodMap.get(clazz.getCanonicalName());
244+
if (methodHashMap != null) {
245+
method = methodMap.get(clazz.getCanonicalName()).get(methodName);
246+
} else {
247+
methodHashMap = new HashMap<>();
248+
methodMap.put(clazz.getCanonicalName(), methodHashMap);
249+
}
250+
if (method == null) {
251+
method = findMethod(clazz, methodName);
252+
if (method != null) {
253+
methodHashMap.put(methodName, method);
254+
}
255+
}
256+
return method;
257+
}
258+
259+
260+
private static Method findMethod(Class clazz, String methodName) {
261+
Method method;
262+
try {
263+
method = clazz.getMethod(methodName);
264+
} catch (NoSuchMethodException e) {
265+
method = findDeclaredMethod(clazz, methodName);
266+
}
267+
return method;
268+
}
269+
270+
private static Method findDeclaredMethod(Class clazz, String methodName) {
271+
Method method = null;
272+
try {
273+
method = clazz.getDeclaredMethod(methodName);
274+
method.setAccessible(true);
275+
} catch (NoSuchMethodException e) {
276+
if (clazz.getSuperclass() != null) {
277+
method = findDeclaredMethod(clazz.getSuperclass(), methodName);
278+
}
279+
}
280+
return method;
281+
}
282+
283+
215284
private static void setDrawable(Drawable drawable, View view, TypedArray otherTa, TypedArray typedArray){
216285

217286
if(view instanceof TextView){

libraryx/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@
205205
<enum name="right" value="4" />
206206
<enum name="bottom" value="8" />
207207
</attr>
208+
209+
<attr name="bl_function" format="string"/>
208210
</declare-styleable>
209211

210212
<declare-styleable name="bl_anim">

0 commit comments

Comments
 (0)