0

我想通过使用来自服务器的浮点值来动态创建矩形形状。形状应该是精确的,如果任何值是 25.2,另一个是 25.3,那么 25.3 应该看起来更大,就像我们在图表中看到的那样。那么有什么方法可以实现这一点吗?这是图像:

在此处输入图像描述

我试图通过使用这个来改变视图大小:

itemView.tv_value.layoutParams = LinearLayout.LayoutParams(width,height)

但这似乎只接受整数值,如果 float 使用 double 转换为 int ,那么它将四舍五入到最接近的数字并且它将不起作用。

如何通过使用画布或视图来实现这一点?

4

1 回答 1

2

首先width还是height任何观点都不能被float看重。

您可以int根据屏幕像素浮点值比率设置值。

适配器生成图形的布局...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:layout_marginLeft="5dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="0.8"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:orientation="vertical"
        android:gravity="center_vertical|center_horizontal|left"
        android:id="@+id/lo_dynamic_view_container">

    </LinearLayout>

    <TextView
        android:id="@+id/tv_chart_value"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:minHeight="50dp"
        android:textColor="#000"
        android:gravity="center_horizontal|center_vertical"
        android:layout_height="match_parent"/>
</LinearLayout>

产生口粮

private float getRatio(int width, float value, float highestValue){
   float result = 0;
   result = ( (float)width/highestValue) * value;
   Log.e("Result", "width: "+ width +" "+(int) Math.floor(result)+"");
   return result;
}

结合 Activity 和适配器类

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;

ArrayList<Data> listData = new ArrayList<>();
BarChartAdapter barChartAdapter;

int[] colors = {Color.GREEN, Color.CYAN, Color.MAGENTA, Color.RED };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.rv_bar_chart);

    listData.add(new Data(8.0f,Color.GREEN));
    listData.add(new Data(4.0f,Color.CYAN));
    listData.add(new Data(2.0f,Color.MAGENTA));

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    barChartAdapter = new BarChartAdapter(listData);
    recyclerView.setAdapter(barChartAdapter);
}

public class BarChartAdapter extends RecyclerView.Adapter<BarChartAdapter.MyViewHolder> {

    ArrayList<Data> listData = new ArrayList<>();

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        LinearLayout layout;
        public MyViewHolder(View v) {
            super(v);
            textView = v.findViewById(R.id.tv_chart_value);
            layout = (LinearLayout) v.findViewById(R.id.lo_dynamic_view_container);
        }
    }

    public BarChartAdapter(ArrayList<Data> listData) {
        this.listData = listData;
    }


    @Override
    public BarChartAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View v = (View) LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.barchart_layout, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.textView.setText(listData.get(position).value+"");

        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        width = width - (100/width)*80;

        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        TextView tv = new TextView(MainActivity.this);
        tv.setLayoutParams(lparams);
        tv.setWidth((int) Math.floor( getRatio(width, listData.get(position).value,getHighestValue(listData))));
        float redious [] = { 0, 0, 8.3f, 8.5f, 8.2f, 8.9f, 0, 0 };
        ShapeDrawable shape = new ShapeDrawable (new RoundRectShape(redious,null,null));
        shape.getPaint().setColor(listData.get(position).color);

        //shape.getPaint().setColor(colors[new Random().nextInt((colors.length-1) - 0 + 1) + 0]);
        //shape.getPaint().setColor(Color.GREEN);

        tv.setBackground(shape);
        holder.layout.addView(tv);

    }

    @Override
    public int getItemCount() {
        return listData.size();
    }
}



private float getRatio(int width, float value, float highestValue){
    float result = 0;
    result = ( (float)width/highestValue) * value;
    Log.e("Result", "width: "+ width +" "+(int) Math.floor(result)+"");
    return result;
}

private float getHighestValue(ArrayList<Data> listData){
    float result = 0.0f;

    if(listData!=null){
        if(listData.size()>0){
            for (int i = 0; i < listData.size(); i++) {
                result = listData.get(i).value>result?listData.get(i).value:result;
            }
        }
    }
    return result;
}


class Data{
    float value;
    int color;

    public Data(float value, int color) {
        this.value = value;
        this.color = color;
    }
}
}

屏幕截图在此处输入图像描述

GitHub 上的完整项目链接

于 2020-01-01T07:37:19.237 回答