DJh*_*hon 6 android dynamic-data line draw
我想绘制一个纯动态视图,如下图所示
我有两个arraylist
List<String> type and List<Float> level;
Run Code Online (Sandbox Code Playgroud)
type具有名称(max,type1,type2等),level具有类型的标记值
level将始终位于0到1之间,type将是一个字符串,level和type的值将来自服务器.我们有两个固定标签 - 最小和最大.
假设我得到.4表示最小值,而.5表示最大值来自服务器,那么所有类型(type1,type2,type3等)将介于.4和.5之间.然后所有其他类型应该像弯曲线一样排列,但是如果我们获得min的值是.001和最大.9那么我们有足够的空间来显示其余的标签,在这种情况下我们不需要通过弯曲显示线或标记.但我不知道如何实现它或从我可以开始的地方.任何帮助将非常感激.在此先感谢所有人.
如果以上设计有点复杂,那么请给我一些参考或链接以实现以下设计.

如果我能够做到这个更简单的(图片上方),那将是非常有利的.
我在onCreate()块中尝试过以下代码.
ViewTreeObserver viewTreeObserver = viewbar.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint({ "NewApi", "ResourceAsColor" })
@Override
public void onGlobalLayout() {
viewbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
viewWidth = viewbar.getWidth();
viewHeight = viewbar.getHeight();
DefineType definetype = new DefineType();
float maxvalue = Collections.max(definetype.frameCalLevels);
float minvalue = Collections.min(definetype.frameCalLevels);
min.setText(definetype.frameCalType.get(0).toString());
max.setText(definetype.frameCalType.get(4).toString());
float density = getApplicationContext().getResources().getDisplayMetrics().density;
int[] posXY = new int[2];
viewbar.getLocationOnScreen(posXY);
int x = posXY[0];
int y = posXY[1];
DrawView drawView;
drawView = new DrawView(MainActivity.this, x, y,density);
//drawView.setBackgroundColor(Color.WHITE);
drawView.setX((float)((x*density/160))+viewWidth+180);
drawView.setX((float) ((float)((y*density/160))));
drawView.invalidate();
ll.addView(drawView);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的内部类绘制视图如下
class DrawView extends View {
Paint paint = new Paint();
float mx, my, mdensity;
Paint mBGPaint, mTXTPaint,mLINEPaint,mBRDPaint;
public DrawView(Context context, float x, float y, float density) {
super(context);
paint.setColor(Color.RED);
paint.setStrokeWidth(8);
paint.setStyle(Paint.Style.STROKE);
mx = x;
my = y;
mdensity = density;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
init();
mLINEPaint.setStrokeWidth(8);
//draw rect
canvas.drawRect(100,100,200,200,mBGPaint);
//draw rect border
canvas.drawRect(100,100,200,200,mBRDPaint);
//draw text
canvas.drawText("min", 250, 460, mTXTPaint);
//draw line
canvas.drawLine(50, 150, 100, 150, mLINEPaint);
}
@SuppressLint("ResourceAsColor")
public void init() {
//rectangle background
mBGPaint = new Paint();
mBGPaint.setColor(0xFF0000FF);
//your text
mTXTPaint = new Paint();
mTXTPaint.setColor(android.R.color.holo_blue_light);
//your line
mLINEPaint = new Paint();
mLINEPaint.setColor(0xFFFF00FF);
//rectangle border
mBRDPaint = new Paint();
mBRDPaint.setStyle(Paint.Style.STROKE);
mBRDPaint.setStrokeWidth(10);
mBRDPaint.setColor(0xFFFFFF00);
}
}
Run Code Online (Sandbox Code Playgroud)
我的XML设计如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/ll">
<View
android:id="@+id/view"
android:layout_width="70dp"
android:layout_height="300dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:background="@drawable/rect" >
</View>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我将使用自定义 View 和自定义 onDraw:
那是,
public class myView extended View {
public myView(Context ctx) {
super(ctx);
init();
}
public void init(){
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//loop here
canvas.drawLine(0, 0, 20, 20, paint);//your some positions.
canvas.drawRect(....)
canvas.drawText(...)
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 你的第二个例子:
init() {
//rectangle background
mBGPaint = new Paint();
mBGPaint.setColor(0xFF0000FF);
//your text
mTXTPaint = new Paint();
mTXTPaint.setColor(0xFFFFFFFF);
//your line
mLINEPaint = new Paint();
mLINEPaint.setColor(0xFFFF00FF);
//rectangle border
mBRDPaint = new Paint();
mBRDPaint.setStyle(Style.STROKE);
mBRDPaint.setStrokeWidth(10);
mBRDPaint.setColor(0xFFFFFF00);
}
onDraw(...) {
//draw rect
canvas.drawRect(100,100,200,200,mBGPaint);
//draw rect border
canvas.drawRect(100,100,200,200,mBRDPaint);
//draw text
canvas.drawRect(100,100,mTXTPaint);
//draw line
canvas.drawLine(50, 150, 100, 150, mLINEPaint);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
452 次 |
| 最近记录: |