首先看效果,数字图片+阴影图片组成如下效果(推测搞一套类似的字体也可以实现),实际实现逻辑是将图片逐个添加进Layout中(使用Adapter实现View的复用)。
Screenshot_20230219_193258.png
用到的资源图片示例如下:0和0的阴影是下图这样的(图片宽高:90*102)。
my_assets_zero.png
my_assets_zero_shadow.png
代码如下:
<com.hzc.yyh.imagetext.AssetRelativeLayout
android:id="@+id/shouyilv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp" />
设置数据:
((AssetRelativeLayout)findViewById(R.id.shouyilv)).setContent("1,023,456,789.0%");
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/num_rl"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/shadow_iv"
android:scaleType="fitCenter"
android:layout_height="56dp"
android:layout_width="48dp"
android:contentDescription="@null"
android:background="@drawable/my_assets_comma" />
<ImageView
android:id="@+id/num_iv"
android:scaleType="fitCenter"
android:layout_height="56dp"
android:layout_width="48dp"
android:background="@drawable/my_assets_comma_shadow"
android:contentDescription="@null" />
</RelativeLayout>
/**
*
*/
package com.hzc.yyh.imagetext;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.hzc.yyh.R;
import java.util.ArrayList;
import java.util.List;
/**
* 数字转图片显示
*/
public class AssetRelativeLayout extends RelativeLayout {
private Context context;
private String[] itemNum = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", ".", "%", "+", "-"};
private int[] itemIcon = {
R.drawable.my_assets_zero, R.drawable.my_assets_one,
R.drawable.my_assets_two, R.drawable.my_assets_three,
R.drawable.my_assets_four, R.drawable.my_assets_five,
R.drawable.my_assets_six, R.drawable.my_assets_seven,
R.drawable.my_assets_eight, R.drawable.my_assets_nine,
R.drawable.my_assets_comma, R.drawable.my_assets_dot,
R.drawable.my_assets_percentage, R.drawable.my_assets_add,
R.drawable.my_assets_minus};
private int[] itemIconShadow = {
R.drawable.my_assets_zero_shadow, R.drawable.my_assets_one_shadow,
R.drawable.my_assets_two_shadow, R.drawable.my_assets_three_shadow,
R.drawable.my_assets_four_shadow, R.drawable.my_assets_five_shadow,
R.drawable.my_assets_six_shadow, R.drawable.my_assets_seven_shadow,
R.drawable.my_assets_eight_shadow, R.drawable.my_assets_nine_shadow,
R.drawable.my_assets_comma_shadow, R.drawable.my_assets_dot_shadow,
R.drawable.my_assets_percentage_shadow, R.drawable.my_assets_add_shadow,
R.drawable.my_assets_minus_shadow};
public AssetRelativeLayout(Context context) {
super(context);
this.context = context;
}
public AssetRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
/**
* 设置内容字符显示为图片
*/
@SuppressLint("NewApi")
public void setContent(String content) {
if (content == null || content.equals("")) {
return;
}
AssetRelativeLayout.this.removeAllViewsInLayout();
content = new StringBuilder(content).reverse().toString();
String[] nums = content.split("");
if (nums.length == 0) {
return;
}
//获取字符对应的图片
int number = 0;
int length = nums.length;
int itemLength = itemNum.length;
List<NumShadow> initData = new ArrayList<NumShadow>();
for (int i = 0; i < length; i++) {
if (nums[i].equals(",") || nums[i].equals(".")) {
number++;
}
for (int j = 0; j < itemLength; j++) {
if (nums[i].equals(itemNum[j])) {
NumShadow ns = new NumShadow("" + nums[i], itemIcon[j], itemIconShadow[j]);
initData.add(ns);
}
}
}
if (initData.size() == 0) {
return;
}
int marginValue = 0;
int size = initData.size();
boolean isNum = true;
//添加View 适配数据
NumShadowAdapter adapter = new NumShadowAdapter(context, initData);
for (int i = 0; i < size; i++) {
View view = adapter.getView(i, null, null);
ImageView num_iv = (ImageView) view.findViewById(R.id.num_iv);
ImageView shadow_iv = (ImageView) view.findViewById(R.id.shadow_iv);
LayoutParams layoutParams = (LayoutParams) num_iv.getLayoutParams();
if (layoutParams != null) {
//最右边的边距 = 个数*每一个数字的宽度减半 - (逗号分号个数+最左边数字1位)*逗号or分号宽度的1/4
int allLeftMargin = (size - 1) * (layoutParams.width / 2) - (number + 1) * (layoutParams.width / 4);
if (i == 0) {
layoutParams.leftMargin = allLeftMargin;
marginValue = allLeftMargin;
} else {
if (initData.get(i).num.equals(",") || initData.get(i).num.equals(".")) {
layoutParams.leftMargin = marginValue - layoutParams.width / 2;
marginValue = marginValue - layoutParams.width / 2;
isNum = false;
} else if (initData.get(i).num.equals("%")) {
layoutParams.leftMargin = marginValue - layoutParams.width / 2 + layoutParams.width / 4;
marginValue = marginValue - layoutParams.width / 2 + layoutParams.width / 4;
isNum = false;
} else {
if (!isNum) {
layoutParams.leftMargin = marginValue - layoutParams.width / 2 + layoutParams.width / 4;
marginValue = marginValue - layoutParams.width / 2 + layoutParams.width / 4;
} else {
layoutParams.leftMargin = marginValue - layoutParams.width / 2;
marginValue = marginValue - layoutParams.width / 2;
}
isNum = true;
}
}
num_iv.setLayoutParams(layoutParams);
shadow_iv.setLayoutParams(layoutParams);
AssetRelativeLayout.this.addView(view);
}
}
}
private class NumShadow {
String num;
int numIcon;
int numShadowIcon;
/**
* @param num
* @param numIcon
* @param numShadowIcon
*/
public NumShadow(String num, int numIcon, int numShadowIcon) {
super();
this.num = num;
this.numIcon = numIcon;
this.numShadowIcon = numShadowIcon;
}
}
private class NumShadowAdapter extends BaseAdapter {
private Context context;
private List<NumShadow> listData;
public NumShadowAdapter(Context context, List<NumShadow> listData) {
this.context = context;
this.listData = listData;
}
@Override
public int getCount() {
return listData.size(); //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Object getItem(int position) {
return listData.get(position); //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public long getItemId(int position) {
return position; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.num_shadow, null);
holder.numIcon = (ImageView) convertView.findViewById(R.id.num_iv);
holder.numShadowIcon = (ImageView) convertView.findViewById(R.id.shadow_iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.numIcon.setBackgroundResource(listData.get(position).numIcon);
holder.numShadowIcon.setBackgroundResource(listData.get(position).numShadowIcon);
return convertView;
}
private class ViewHolder {
public ImageView numIcon;
public ImageView numShadowIcon;
}
}
}
网友评论