欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)折線圖小工具

 更新時間:2022年09月14日 17:29:02   作者:Rose?J  
這篇文章主要為大家詳細介紹了Android實現(xiàn)折線圖小工具,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)折線圖小工具的具體代碼,供大家參考,具體內(nèi)容如下

1.LineChart類

public class LineChart extends View {
? ? private Paint XPaint;
? ? private Paint YPaint;
? ? private Paint pointPaint;
? ? private Paint circlePaint;
? ? private Paint shapePaint;
? ? private Paint effectPaint, effectPaint1;
? ? private float yandianx, yuandiany, height, wigth;
? ? private Context context;
? ? private String ysplit[];
? ? private String unit;
? ? private float max;
? ? private int textSize = 7;
? ? private int margin = 20;
? ? private float gao;
? ? private boolean start = false;
? ? private Map<String, Float> mapx;

? ? public LineChart(Context context) {
? ? ? ? super(context);
? ? ? ? this.context = context;
? ? }


? ? public LineChart(Context context, AttributeSet attrs) {
? ? ? ? super(context, attrs);
? ? ? ? this.context = context;
? ? }

? ? public LineChart(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(context, attrs, defStyleAttr);
? ? ? ? this.context = context;

? ? }

? ? @TargetApi(Build.VERSION_CODES.LOLLIPOP)
? ? public LineChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
? ? ? ? super(context, attrs, defStyleAttr, defStyleRes);
? ? ? ? this.context = context;
? ? }

? ? private void initView() {
? ? ? ? XPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? XPaint.setAntiAlias(true);
? ? ? ? XPaint.setColor(Color.parseColor("#1e90ff"));
? ? ? ? XPaint.setStrokeWidth(dp2px(1));

? ? ? ? YPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? YPaint.setAntiAlias(true);
? ? ? ? YPaint.setColor(Color.BLACK);
? ? ? ? YPaint.setStrokeWidth(dp2px(2));

? ? ? ? pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? pointPaint.setAntiAlias(true);
? ? ? ? pointPaint.setColor(Color.BLACK);
? ? ? ? pointPaint.setStyle(Paint.Style.STROKE);
? ? ? ? pointPaint.setStrokeWidth(dp2px(1));

? ? ? ? circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? circlePaint.setAntiAlias(true);
? ? ? ? circlePaint.setColor(Color.WHITE);
? ? ? ? circlePaint.setStyle(Paint.Style.FILL);
? ? ? ? circlePaint.setStrokeWidth(1);


? ? ? ? shapePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? shapePaint.setAntiAlias(true);
? ? ? ? shapePaint.setColor(Color.TRANSPARENT);
? ? ? ? shapePaint.setStyle(Paint.Style.FILL);
? ? ? ? shapePaint.setStrokeWidth(1);

? ? ? ? effectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? effectPaint.setAntiAlias(true);
? ? ? ? effectPaint.setColor(Color.BLACK);
? ? ? ? effectPaint.setStyle(Paint.Style.FILL);
? ? ? ? effectPaint.setStrokeWidth(1);
? ? ? ? effectPaint.setTextSize(sp2px(textSize));
? ? ? ? effectPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
? ? ? ? effectPaint1.setAntiAlias(true);
? ? ? ? effectPaint1.setColor(Color.BLACK);
? ? ? ? effectPaint1.setStyle(Paint.Style.FILL);
? ? ? ? effectPaint1.setStrokeWidth(1);
? ? ? ? effectPaint1.setTextSize(sp2px(9));
? ? }

? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? if (start) {
? ? ? ? ? ? yandianx = dp2px(margin);
? ? ? ? ? ? yuandiany = getMeasuredHeight() - dp2px(margin);
? ? ? ? ? ? wigth = getMeasuredWidth() - dp2px(margin * 2);
? ? ? ? ? ? height = getMeasuredHeight() - dp2px(margin * 2);
? ? ? ? ? ? float point[] = new float[]{yandianx, yuandiany, yandianx, yandianx, yandianx - dp2px(3), yandianx + dp2px(textSize / 2)};
? ? ? ? ? ? canvas.drawLines(point, 0, 4, YPaint);
? ? ? ? ? ? canvas.drawLines(point, 2, 4, YPaint);
? ? ? ? ? ? canvas.drawLine(yandianx, yandianx, yandianx + dp2px(3), yandianx + dp2px(textSize / 2), YPaint);
? ? ? ? ? ? canvas.drawLine(yandianx, yuandiany, yandianx + wigth, yuandiany, YPaint);
? ? ? ? ? ? canvas.drawLine(yandianx + wigth, yuandiany, yandianx + wigth - dp2px(textSize / 2), yuandiany - dp2px(3), YPaint);
? ? ? ? ? ? canvas.drawLine(yandianx + wigth, yuandiany, yandianx + wigth - dp2px(textSize / 2), yuandiany + dp2px(3), YPaint);
? ? ? ? ? ? canvas.drawText("0", yandianx - sp2px(textSize) - dp2px(2), yuandiany + sp2px(textSize) + dp2px(2), effectPaint);
? ? ? ? ? ? if (null != unit) {
? ? ? ? ? ? ? ? canvas.drawText(unit, yandianx - sp2px(textSize) * unit.length() / 2, yuandiany - height - dp2px(3), effectPaint);
? ? ? ? ? ? }
? ? ? ? ? ? if (null != ysplit && ysplit.length > 0) {
? ? ? ? ? ? ? ? gao = height / (ysplit.length + 1);
? ? ? ? ? ? ? ? for (int i = 0; i < ysplit.length; i++) {
? ? ? ? ? ? ? ? ? ? float a = Float.parseFloat(ysplit[i]);
? ? ? ? ? ? ? ? ? ? if (max < a) {
? ? ? ? ? ? ? ? ? ? ? ? max = a;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? canvas.drawLine(yandianx, yuandiany - (i + 1) * gao, yandianx + dp2px(3), yuandiany - (i + 1) * gao, YPaint);
? ? ? ? ? ? ? ? ? ? canvas.drawText(ysplit[i], yandianx - (sp2px(textSize) * (ysplit[i].length())), yuandiany - (i + 1) * gao + sp2px(textSize / 2), effectPaint);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (mapx.size() > 0) {
? ? ? ? ? ? ? ? float kuan = wigth / (mapx.size() + 1);
? ? ? ? ? ? ? ? Object o[] = mapx.keySet().toArray();
? ? ? ? ? ? ? ? for (int i = 0; i < o.length; i++) {
? ? ? ? ? ? ? ? ? ? String s = o[i].toString();
? ? ? ? ? ? ? ? ? ? float x = yandianx + (i + 1) * kuan;
? ? ? ? ? ? ? ? ? ? float y = yuandiany - (height - gao) / max * mapx.get(o[i]);
? ? ? ? ? ? ? ? ? ? canvas.drawLine(x, yuandiany, x, yuandiany - dp2px(3), YPaint);
? ? ? ? ? ? ? ? ? ? canvas.drawText(s, x - (sp2px(textSize) * (s.length() / 2)), yuandiany + sp2px(textSize) + dp2px(3), effectPaint);
? ? ? ? ? ? ? ? ? ? if (i > 0) {
? ? ? ? ? ? ? ? ? ? ? ? canvas.drawLine(yandianx + i * kuan, yuandiany - (height - gao) / max * mapx.get(o[i - 1]), x, y, XPaint);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? int size = dp2px(3);
? ? ? ? ? ? ? ? ? ? for (int i1 = 0; i1 < (x - yandianx) / size; i1++) {
? ? ? ? ? ? ? ? ? ? ? ? if (i1 % 2 == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawLine(i1 * size + yandianx, y, (i1 + 1) * size + yandianx, y, shapePaint);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? for (int i1 = 0; i1 < (yuandiany - y) / size; i1++) {
? ? ? ? ? ? ? ? ? ? ? ? if (i1 % 2 == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? canvas.drawLine(x, yuandiany - i1 * size, x, yuandiany - (i1 + 1) * size, shapePaint);
? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? String text = mapx.get(o[i]).toString();
? ? ? ? ? ? ? ? ? ? canvas.drawText(text, x - text.length() * sp2px(textSize / 4), y - dp2px(3), effectPaint1);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? for (int i = 0; i < o.length; i++) {
? ? ? ? ? ? ? ? ? ? float x = yandianx + (i + 1) * kuan;
? ? ? ? ? ? ? ? ? ? float y = yuandiany - (height - gao) / max * mapx.get(o[i]);
? ? ? ? ? ? ? ? ? ? canvas.drawCircle(x, y, dp2px(3), circlePaint);
? ? ? ? ? ? ? ? ? ? canvas.drawCircle(x, y, dp2px(3), pointPaint);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }


? ? public void startDraw(Map<String, Float> mapx, String[] ysplit, String unit, int margin, int textSize) {
? ? ? ? start = true;
? ? ? ? this.mapx = mapx;
? ? ? ? this.ysplit = ysplit;
? ? ? ? this.unit = unit;
? ? ? ? this.textSize = textSize;
? ? ? ? this.margin = margin;
? ? ? ? initView();
? ? ? ? invalidate();
? ? }

? ? /**
? ? ?* sp轉(zhuǎn)換成px
? ? ?*/
? ? private int sp2px(float spValue) {
? ? ? ? float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
? ? ? ? return (int) (spValue * fontScale + 0.5f);
? ? }


? ? /**
? ? ?* dp轉(zhuǎn)換成px
? ? ?*/
? ? private int dp2px(float dpValue) {
? ? ? ? float scale = context.getResources().getDisplayMetrics().density;
? ? ? ? return (int) (dpValue * scale + 0.5f);
? ? }
}

2.Fragment代碼

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical">

? ? <com.hnucm18jr.myapplication.LineChart
? ? ? ? android:id="@+id/lineChart"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

2.邏輯代碼

public class Zt_fragment extends Fragment {

? ? LineChart mLineChart;
? ? Calendar calendar = Calendar.getInstance();
? ? @Override
? ? public View onCreateView(LayoutInflater inflater, ViewGroup container,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Bundle savedInstanceState) {
? ? ? ? // Inflate the layout for this fragment
? ? ? ? return inflater.inflate(R.layout.fragment_zt_fragment, container, false);
? ? }

? ? @Override
? ? public void onActivityCreated(@Nullable Bundle savedInstanceState) {
? ? ? ? super.onActivityCreated(savedInstanceState);
? ? ? ? mLineChart=getActivity(). findViewById(R.id.lineChart);
? ? ? ? //月
? ? ? ? int month = calendar.get(Calendar.MONTH)+1;
//日
? ? ? ? int day = calendar.get(Calendar.DAY_OF_MONTH);
? ? ? ? Log.i("TIME","-------"+month+day);

? ? ? ? Map<String ,Float> map=new LinkedHashMap<>() ;//一定要用有序的Map
? ? ? ? int min=6;
? ? ? ? int max=18;
? ? ? ? Random random = new Random();
? ? ? ? float num1 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? float num2 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? float num3 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? float num4 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? float num5 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? float num6 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? float num7 = random.nextInt(max)%(max-min+1) + min;
? ? ? ? if ((day-6)<=0){

? ? ? ? ? ? String s1= (month-1) + "." + (30+day-6);
? ? ? ? ? ? map.put(s1,num1);
? ? ? ? }else {
? ? ? ? ? ? String s1= month + "." + (day-6);
? ? ? ? ? ? map.put(s1,num1);
? ? ? ? }
? ? ? ? if ((day-5)<=0){

? ? ? ? ? ? String s2= (month-1) + "." + (30+day-5);

? ? ? ? ? ? map.put(s2,num2);
? ? ? ? }else {
? ? ? ? ? ? String s2= month + "." + (day-5);

? ? ? ? ? ? map.put(s2,num2);
? ? ? ? }
? ? ? ? if ((day-4)<=0){

? ? ? ? ? ? String s3= (month-1) + "." + (30+day-4);

? ? ? ? ? ? map.put(s3,num3);
? ? ? ? }else {
? ? ? ? ? ? String s3= month + "." + (day-4);

? ? ? ? ? ? map.put(s3,num3);
? ? ? ? }
? ? ? ? if ((day-3)<=0){

? ? ? ? ? ? String s4= (month-1) + "." + (30+day-3);

? ? ? ? ? ? map.put(s4,num4);
? ? ? ? }else {
? ? ? ? ? ? String s4= month + "." + (day-3);

? ? ? ? ? ? map.put(s4,num4);
? ? ? ? }
? ? ? ? if ((day-2)<=0){

? ? ? ? ? ? String s5= (month-1) + "." + (30+day-2);

? ? ? ? ? ? map.put(s5,num5);
? ? ? ? }else {
? ? ? ? ? ? String s5= month + "." + (day-2);

? ? ? ? ? ? map.put(s5,num5);
? ? ? ? }
? ? ? ? if ((day-1)<=0){

? ? ? ? ? ? String s6= (month-1) + "." + (30+day-1);

? ? ? ? ? ? map.put(s6,num6);
? ? ? ? }else {
? ? ? ? ? ? String s6= month + "." + (day-1);

? ? ? ? ? ? map.put(s6,num6);
? ? ? ? }

? ? ? ? String s7= month + "." + day;


? ? ? ? map.put(s7,num7);
? ? ? ? String[] a=new String[]{"06","12","18","24"};

? ? ? ? mLineChart.startDraw(map,a,"小時",40,10);//map為橫坐標數(shù)據(jù)和點數(shù)據(jù),a為縱坐標刻度(為數(shù)字類型的字符串,m/s為縱坐標單位,40為坐標軸距離邊界的位置dp,14是坐標軸字體大?。?

? ? }
}

3.Activity代碼

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity">

? ? <FrameLayout
? ? ? ? android:id="@+id/frameLayout"
? ? ? ? android:layout_width="0dp"
? ? ? ? android:layout_height="300dp"
? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

2.邏輯代碼

public class MainActivity extends AppCompatActivity {


? ? Zt_fragment mZt_fragment=new Zt_fragment();
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,mZt_fragment).commit();


? ? }
}

運行

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中TabLayout添加小紅點的示例代碼

    Android中TabLayout添加小紅點的示例代碼

    本篇文章主要介紹了Android中TabLayout添加小紅點的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Android通過命令連接wifi的方法(解決usb不能用問題)

    Android通過命令連接wifi的方法(解決usb不能用問題)

    這篇文章主要介紹了Android通過命令連接wifi的方法(解決usb不能用的情況),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • Android動態(tài)替換Application實現(xiàn)

    Android動態(tài)替換Application實現(xiàn)

    這篇文章主要介紹了Android動態(tài)替換Application實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Flutter學習LogUtil封裝與實現(xiàn)實例詳解

    Flutter學習LogUtil封裝與實現(xiàn)實例詳解

    這篇文章主要為大家介紹了Flutter學習LogUtil封裝與實現(xiàn)實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android中刷新界面的二種方法

    Android中刷新界面的二種方法

    這篇文章主要介紹了Android中刷新界面的二種方法,本文使用Handler、postInvalidate兩種方法實現(xiàn)界面刷新,需要的朋友可以參考下
    2014-10-10
  • 談談RxJava2中的異常及處理方法

    談談RxJava2中的異常及處理方法

    這篇文章主要給大家介紹了關(guān)于RxJava2中異常及處理方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用RxJava2具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-08-08
  • Android開發(fā)案例手冊Application跳出dialog

    Android開發(fā)案例手冊Application跳出dialog

    這篇文章主要為大家介紹了Android開發(fā)案例手冊Application跳出dialog,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Android AMS啟動詳解

    Android AMS啟動詳解

    這篇文章主要介紹了Android AMS啟動的相關(guān)資料,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android 開發(fā)環(huán)境配置問題

    Android 開發(fā)環(huán)境配置問題

    手機軟件開發(fā)IDE,本人在同時使用 Eclipse 和 Netbeans 后,感覺 Eclipse 更適合自己
    2012-04-04
  • Android自定義ViewFlipper實現(xiàn)滾動效果

    Android自定義ViewFlipper實現(xiàn)滾動效果

    這篇文章主要為大家詳細介紹了Android自定義ViewFlipper實現(xiàn)滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08

最新評論