Skip to content

Commit 0b8564c

Browse files
committed
add RippleMenuView
1 parent 49dad44 commit 0b8564c

File tree

6 files changed

+394
-0
lines changed

6 files changed

+394
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cn.beingyi.androidcore.model;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
6+
import com.hbv.app.dialog.LoadingDialog;
7+
8+
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.InputStream;
11+
import java.net.URL;
12+
import java.net.URLConnection;
13+
14+
/**
15+
* author: zhengyu
16+
* date: 2020/12/30 20:50
17+
*/
18+
public class FileDownloader {
19+
Activity activity;
20+
String destPath;
21+
String url;
22+
OnDownloadResultListener onDownloadResultListener;
23+
24+
public FileDownloader(Context context, String destPath, String url, OnDownloadResultListener onDownloadResultListener) {
25+
this.activity = (Activity) context;
26+
this.destPath = destPath;
27+
this.url = url;
28+
this.onDownloadResultListener = onDownloadResultListener;
29+
30+
LoadingDialog loadingDialog=new LoadingDialog(activity);
31+
loadingDialog.show();
32+
new Thread(){
33+
@Override
34+
public void run() {
35+
super.run();
36+
download(url,new File(destPath));
37+
activity.runOnUiThread(new Runnable() {
38+
@Override
39+
public void run() {
40+
loadingDialog.dismiss();
41+
}
42+
});
43+
}
44+
}.start();
45+
}
46+
47+
48+
private void download(String urlString, File destFile) {
49+
try {
50+
URL url = new URL(urlString);
51+
URLConnection conn = url.openConnection();
52+
InputStream inStream = conn.getInputStream();
53+
FileOutputStream fs = new FileOutputStream(destFile);
54+
byte[] buffer = new byte[1204];
55+
int length;
56+
int byteread = 0;
57+
while ((byteread = inStream.read(buffer)) != -1) {
58+
fs.write(buffer, 0, byteread);
59+
}
60+
inStream.close();
61+
fs.close();
62+
activity.runOnUiThread(new Runnable() {
63+
@Override
64+
public void run() {
65+
onDownloadResultListener.onSuccess(destFile);
66+
}
67+
});
68+
} catch (Exception e) {
69+
e.printStackTrace();
70+
activity.runOnUiThread(new Runnable() {
71+
@Override
72+
public void run() {
73+
onDownloadResultListener.onFailure();
74+
}
75+
});
76+
}
77+
}
78+
79+
public interface OnDownloadResultListener{
80+
void onSuccess(File file);
81+
void onFailure();
82+
}
83+
84+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cn.beingyi.androidcore.model;
2+
3+
import android.graphics.Point;
4+
import android.text.SpannableString;
5+
import android.text.Spanned;
6+
import android.text.style.ForegroundColorSpan;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
12+
/**
13+
* author: zhengyu
14+
* date: 2020/12/4 13:56
15+
*/
16+
public class RichText {
17+
18+
List<TextBean> stringList =new ArrayList<>();
19+
20+
public RichText() {
21+
}
22+
23+
public RichText append(String text, int color){
24+
stringList.add(new TextBean(text,color));
25+
return this;
26+
}
27+
28+
public SpannableString build(){
29+
HashMap<Point, Integer> colorMap = new HashMap<>();
30+
StringBuilder sb = new StringBuilder();
31+
int position = 0;
32+
for (TextBean bean: stringList) {
33+
int color = bean.color;
34+
String text=bean.text;
35+
sb.append(text);
36+
Point point = new Point(position, position + text.length());
37+
position += text.length();
38+
colorMap.put(point, color);
39+
}
40+
SpannableString ss = new SpannableString(sb.toString());
41+
for (Point point : colorMap.keySet()) {
42+
ss.setSpan(new ForegroundColorSpan(colorMap.get(point)), point.x, point.y, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
43+
}
44+
return ss;
45+
}
46+
47+
class TextBean{
48+
private String text;
49+
private int color;
50+
51+
public TextBean(String text, int color) {
52+
this.text = text;
53+
this.color = color;
54+
}
55+
56+
public String getText() {
57+
return text;
58+
}
59+
60+
public void setText(String text) {
61+
this.text = text;
62+
}
63+
64+
public int getColor() {
65+
return color;
66+
}
67+
68+
public void setColor(int color) {
69+
this.color = color;
70+
}
71+
}
72+
73+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package cn.beingyi.androidcore.widget;
2+
3+
import android.content.Context;
4+
import android.content.res.TypedArray;
5+
import android.util.AttributeSet;
6+
import android.util.TypedValue;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.ImageView;
11+
import android.widget.LinearLayout;
12+
import android.widget.TextView;
13+
14+
15+
import androidx.annotation.Nullable;
16+
17+
import org.xutils.common.util.DensityUtil;
18+
19+
import cn.beingyi.androidcore.R;
20+
21+
/**
22+
* @author zhengyu
23+
* @date : 2020/7/22 19:88
24+
*/
25+
public class RippleMenuView extends LinearLayout {
26+
27+
public RippleMenuView(Context context, @Nullable AttributeSet attrs) {
28+
super(context, attrs);
29+
init();
30+
initAttr(attrs);
31+
}
32+
33+
public RippleMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
34+
super(context, attrs, defStyleAttr);
35+
init();
36+
initAttr(attrs);
37+
}
38+
39+
Context context;
40+
MaterialRippleLayout parent;
41+
LinearLayout ln_parent;
42+
ImageView img_icon;
43+
TextView tv_label;
44+
TextView tv_value;
45+
ImageView img_arrow;
46+
47+
private void init() {
48+
context = getContext();
49+
setOrientation(VERTICAL);
50+
51+
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
52+
setLayoutParams(lp);
53+
setClickable(true);
54+
55+
View view = LayoutInflater.from(context).inflate(R.layout.item_ripple_menu, null);
56+
parent = view.findViewById(R.id.MaterialRippleLayout_parent);
57+
ln_parent = view.findViewById(R.id.LinearLayout_parent);
58+
img_icon = view.findViewById(R.id.ImageView_icon);
59+
tv_label = view.findViewById(R.id.TextView_label);
60+
tv_value = view.findViewById(R.id.TextView_value);
61+
img_arrow = view.findViewById(R.id.ImageView_arrow);
62+
63+
addView(view);
64+
}
65+
66+
67+
private void initAttr(AttributeSet attrs) {
68+
TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.RippleMenuView);
69+
int icon = array.getResourceId(R.styleable.RippleMenuView_menuIcon, 0);
70+
String label = array.getString(R.styleable.RippleMenuView_menuLabel);
71+
String value = array.getString(R.styleable.RippleMenuView_menuValue);
72+
boolean hasArrow = array.getBoolean(R.styleable.RippleMenuView_hasArrow, false);
73+
74+
if (icon == 0) {
75+
img_icon.setVisibility(GONE);
76+
} else {
77+
img_icon.setImageResource(icon);
78+
img_icon.setVisibility(VISIBLE);
79+
}
80+
81+
tv_label.setText(label);
82+
tv_value.setText(value);
83+
84+
if (value.isEmpty()) {
85+
tv_value.setVisibility(GONE);
86+
}
87+
88+
if (hasArrow) {
89+
img_arrow.setVisibility(VISIBLE);
90+
} else {
91+
img_arrow.setVisibility(INVISIBLE);
92+
}
93+
94+
float iconWidth = array.getLayoutDimension(R.styleable.RippleMenuView_menuIconWidth, 0);
95+
float iconHeight = array.getLayoutDimension(R.styleable.RippleMenuView_menuIconHeight, 0);
96+
97+
{
98+
ViewGroup.LayoutParams lp = img_icon.getLayoutParams();
99+
lp.width = (int) iconWidth;
100+
lp.height = (int) iconHeight;
101+
img_icon.setLayoutParams(lp);
102+
}
103+
int labelTextColor = array.getColor(R.styleable.RippleMenuView_labelTextColor, 0);
104+
tv_label.setTextColor(labelTextColor);
105+
106+
{
107+
float labelTextSize = array.getDimension(R.styleable.RippleMenuView_labelTextSize, 0);
108+
int sp = DensityUtil.px2dip(labelTextSize);
109+
tv_label.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
110+
}
111+
112+
{
113+
float paddingLeft = array.getDimension(R.styleable.RippleMenuView_parentPaddingLeft, 0);
114+
float paddingTop = array.getDimension(R.styleable.RippleMenuView_parentPaddingTop, 0);
115+
float paddingRight = array.getDimension(R.styleable.RippleMenuView_parentPaddingRight, 0);
116+
float paddingBottom = array.getDimension(R.styleable.RippleMenuView_parentPaddingBottom, 0);
117+
ln_parent.setPadding((int) paddingLeft, (int) paddingTop, (int) paddingRight, (int) paddingBottom);
118+
}
119+
120+
121+
array.recycle();
122+
}
123+
124+
125+
@Override
126+
public void setOnClickListener(OnClickListener onClickListener) {
127+
parent.setOnClickListener(onClickListener);
128+
}
129+
130+
public void setIcon(int resid) {
131+
img_icon.setImageResource(resid);
132+
}
133+
134+
public void setLabel(String str) {
135+
tv_label.setText(str);
136+
}
137+
138+
public void setValue(String str) {
139+
tv_value.setText(str);
140+
tv_value.setVisibility(VISIBLE);
141+
}
142+
143+
144+
}
1.64 KB
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<cn.beingyi.androidcore.widget.MaterialRippleLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:id="@+id/MaterialRippleLayout_parent"
5+
android:layout_width="match_parent"
6+
android:layout_height="wrap_content"
7+
app:mrl_rippleAlpha="0.2"
8+
app:mrl_rippleColor="#898888"
9+
app:mrl_rippleHover="true"
10+
app:mrl_rippleOverlay="true"
11+
android:descendantFocusability="blocksDescendants">
12+
13+
14+
<LinearLayout
15+
android:id="@+id/LinearLayout_parent"
16+
android:gravity="center_vertical"
17+
android:orientation="horizontal"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:paddingLeft="10dp"
21+
android:paddingTop="15dp"
22+
android:paddingRight="10dp"
23+
android:paddingBottom="15dp"
24+
android:focusable="false">
25+
26+
27+
<ImageView
28+
android:id="@+id/ImageView_icon"
29+
android:layout_width="22dp"
30+
android:layout_height="22dp"
31+
android:focusable="false"/>
32+
33+
<TextView
34+
android:id="@+id/TextView_label"
35+
android:textColor="@color/black"
36+
android:text="@string/app_name"
37+
android:textSize="13sp"
38+
android:layout_weight="1.0"
39+
android:layout_marginLeft="10dp"
40+
android:layout_width="match_parent"
41+
android:layout_height="wrap_content"
42+
android:focusable="false"/>
43+
44+
<TextView
45+
android:id="@+id/TextView_value"
46+
android:gravity="right|center_vertical"
47+
android:layout_marginRight="10dp"
48+
android:text="@string/app_name"
49+
android:textSize="12sp"
50+
android:textColor="@color/gray"
51+
android:layout_weight="1.0"
52+
android:layout_width="match_parent"
53+
android:layout_height="wrap_content"
54+
android:focusable="false"/>
55+
56+
57+
<ImageView
58+
android:id="@+id/ImageView_arrow"
59+
android:padding="5dp"
60+
android:src="@drawable/ic_right_arrow"
61+
app:tint="#999999"
62+
android:layout_width="20dp"
63+
android:layout_height="20dp"
64+
android:focusable="false"/>
65+
66+
67+
</LinearLayout>
68+
69+
70+
</cn.beingyi.androidcore.widget.MaterialRippleLayout>

0 commit comments

Comments
 (0)