2017-03-16 66 views
0

我使用这个库从GitHub:https://github.com/lzyzsd/CircleProgress刷新在Android中,更新圈子进度条的布局编程

我把ArcProgress到我MainLayout。我试图改变进度条上的值,但我不能在屏幕上看到。如果我检查mArcProgress.getProgress();这给我正确的价值,但酒吧在屏幕上是空的,并没有显示任何进展。

我用努力:

mArcProgress.invalidate(); 

,但不工作。

我发现的唯一方法是用:

finish(); 
startActivity(getIntent()); 

,但工程没有如我所料,因为全屏幕开始,似乎像一个闪烁。

任何人都知道如何从其他活动中更改此视图,并在设置mArcProgress.setProgress(aNumber);时看到更改?

这是ArcProgressFuel

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.RectF; 
import android.os.Bundle; 
import android.os.Parcelable; 

import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 

public class ArcProgressFuel extends View { 
    private Paint paint; 

    private RectF rectF = new RectF(); 

    private float strokeWidth; 

    public int progress; 
    private int max; 
    private int finishedStrokeColor; 
    private int unfinishedStrokeColor; 
    private float arcAngle; 

    Path path; 

    MainPageFragment mMainPageFragment; 

    public static int fuel_to_show; 
    private float arcBottomHeight; 

    private final int default_finished_color = Color.WHITE; 
    private final int default_unfinished_color = Color.rgb(72, 106, 176); 

    private final float default_suffix_padding; 
    private final float default_stroke_width; 
    private final int default_max = 100; 
    private final float default_arc_angle = 360 * 0.5f; //0.8 for speed, 0.5 for fuel 
    private float default_text_size; 
    private final int min_size; 

    private static final String INSTANCE_STATE = "saved_instance"; 
    private static final String INSTANCE_STROKE_WIDTH = "stroke_width"; 

    public static String instance_progress = "progress"; 
    private static final String INSTANCE_MAX = "max"; 
    private static final String INSTANCE_FINISHED_STROKE_COLOR = "finished_stroke_color"; 
    private static final String INSTANCE_UNFINISHED_STROKE_COLOR = "unfinished_stroke_color"; 
    private static final String INSTANCE_ARC_ANGLE = "arc_angle"; 
    private static final String INSTANCE_SUFFIX = "suffix"; 


    public ArcProgressFuel(Context context) { 
     this(context, null); 
    } 

    public ArcProgressFuel(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public ArcProgressFuel(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 

     min_size = (int) Utils.dp2px(getResources(), 100); 
     default_suffix_padding = Utils.dp2px(getResources(), 4); 
     default_stroke_width = Utils.dp2px(getResources(), 4); 

     TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ArcProgress, defStyleAttr, 0); 
     initByAttributes(attributes); 
     attributes.recycle(); 

     initPainters(); 
    } 

    protected void initByAttributes(TypedArray attributes) { 
     finishedStrokeColor = attributes.getColor(R.styleable.ArcProgress_arc_finished_color, default_finished_color); 
     unfinishedStrokeColor = attributes.getColor(R.styleable.ArcProgress_arc_unfinished_color, default_unfinished_color); 

     arcAngle = attributes.getFloat(R.styleable.ArcProgress_arc_angle, default_arc_angle); 
     setMax(attributes.getInt(R.styleable.ArcProgress_arc_max, default_max)); 
     setProgress(attributes.getInt(R.styleable.ArcProgress_arc_progress, 0)); 

     strokeWidth = attributes.getDimension(R.styleable.ArcProgress_arc_stroke_width, default_stroke_width); 

     fuel_to_show=mMainPageFragment.carFuel; 
     setProgress(fuel_to_show); //Aca puedo modificar el valor 
    } 

    protected void initPainters() { 

     paint = new Paint(); 
     paint.setColor(default_unfinished_color); 
     paint.setAntiAlias(true); 
     paint.setStrokeWidth(strokeWidth); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeCap(Paint.Cap.ROUND); 
    } 

    /* @Override 
    public void invalidate() { 
     initPainters(); 
     super.invalidate(); 
     postInvalidate(); 
    }*/ 

    public float getStrokeWidth() { 
     return strokeWidth; 
    } 

    public void setStrokeWidth(float strokeWidth) { 
     this.strokeWidth = strokeWidth; 
     this.invalidate(); 
    } 

    public int getProgress() { 
     return progress; 
    } 

    public void setProgress(int progress) { 
     this.progress = progress; 
     if (this.progress > getMax()) { 
      this.progress %= getMax(); 
     } 
     invalidate(); 
    } 

    public int getMax() { 
     return max; 
    } 

    public void setMax(int max) { 
     if (max > 0) { 
      this.max = max; 
      invalidate(); 
     } 
    } 

    public int getFinishedStrokeColor() { 
     return finishedStrokeColor; 
    } 

    public void setFinishedStrokeColor(int finishedStrokeColor) { 
     this.finishedStrokeColor = finishedStrokeColor; 
     this.invalidate(); 
    } 

    public int getUnfinishedStrokeColor() { 
     return unfinishedStrokeColor; 
    } 

    public void setUnfinishedStrokeColor(int unfinishedStrokeColor) { 
     this.unfinishedStrokeColor = unfinishedStrokeColor; 
     this.invalidate(); 
    } 

    public float getArcAngle() { 
     return arcAngle; 
    } 

    public void setArcAngle(float arcAngle) { 
     this.arcAngle = arcAngle; 
     this.invalidate(); 
    } 

    @Override 
    protected int getSuggestedMinimumHeight() { 
     return min_size; 
    } 

    @Override 
    protected int getSuggestedMinimumWidth() { 
     return min_size; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     rectF.set(strokeWidth/2f, strokeWidth/2f, width - strokeWidth/2f, MeasureSpec.getSize(heightMeasureSpec) - strokeWidth/2f); 
     float radius = width/2f; 
     float angle = (360 - arcAngle)/2f; 
     arcBottomHeight = radius * (float) (1 - Math.cos(angle/180 * Math.PI)); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     float startAngle = 90 - arcAngle/2f; //270 for speed, 90 for fuel 
     float finishedSweepAngle = progress/(float) getMax() * arcAngle; 
     float finishedStartAngle = startAngle; 
     if(progress == 0) finishedStartAngle = 0.01f; 
     paint.setColor(unfinishedStrokeColor); 
     canvas.drawArc(rectF, startAngle, arcAngle, false, paint); 
     paint.setColor(finishedStrokeColor); 
     canvas.drawArc(rectF, finishedStartAngle, finishedSweepAngle, false, paint); 

     if(arcBottomHeight == 0) { 
      float radius = getWidth()/2f; 
      float angle = (360 - arcAngle)/2f; 
      arcBottomHeight = radius * (float) (1 - Math.cos(angle/180 * Math.PI)); 
     } 
    } 

    @Override 
    protected Parcelable onSaveInstanceState() { 
     final Bundle bundle = new Bundle(); 
     bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState()); 
     bundle.putFloat(INSTANCE_STROKE_WIDTH, getStrokeWidth()); 
     bundle.putInt(instance_progress, getProgress()); 
     bundle.putInt(INSTANCE_MAX, getMax()); 
     bundle.putInt(INSTANCE_FINISHED_STROKE_COLOR, getFinishedStrokeColor()); 
     bundle.putInt(INSTANCE_UNFINISHED_STROKE_COLOR, getUnfinishedStrokeColor()); 
     bundle.putFloat(INSTANCE_ARC_ANGLE, getArcAngle()); 
     return bundle; 
    } 

    @Override 
    protected void onRestoreInstanceState(Parcelable state) { 
     if(state instanceof Bundle) { 
      final Bundle bundle = (Bundle) state; 
      strokeWidth = bundle.getFloat(INSTANCE_STROKE_WIDTH); 
      setMax(bundle.getInt(INSTANCE_MAX)); 
      setProgress(bundle.getInt(instance_progress)); 
      finishedStrokeColor = bundle.getInt(INSTANCE_FINISHED_STROKE_COLOR); 
      unfinishedStrokeColor = bundle.getInt(INSTANCE_UNFINISHED_STROKE_COLOR); 
      initPainters(); 
      super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE)); 
      return; 
     } 
     super.onRestoreInstanceState(state); 
    } 

} 

这是我MainLayout里面,这部分我想更新:

    <RelativeLayout 
         android:id="@+id/scale_gaz_inmain" 
         android:layout_width="@dimen/_57sdp" 
         android:layout_centerHorizontal="true" 
         android:layout_below="@+id/icon_gazinmain" 
         android:layout_height="@dimen/_60sdp" 
         > 

         <com.itelma.tele2.ArcProgressFuel 
          android:id="@+id/arc_progress2" 
          android:background="@android:color/transparent" 
          android:layout_width="match_parent" 
          android:scaleX="-1" 
          android:layout_height="match_parent" 
          custom:arc_progress="50" 
          custom:arc_unfinished_color="@color/mDark_gray" 
          custom:arc_finished_color="@color/colorApp" 
          custom:arc_max="100"/> 
        </RelativeLayout> 

所以我主要的Java文件,我有:

ArcProgressFuel mArcProgressFuel; 


mArcProgressFuel=new ArcProgressFuel(this); 
mArcProgressFuel.invalidate(); 
+0

你可以发布你如何设置进度? – X3Btel

+0

@ X3Btel好的,我做到了 – cristian

回答

0

好吧,我可以将它移除,使用以下几行:

mArcProgressFuel=new ArcProgressFuel(this); 

     RelativeLayout relativeLayout2=(RelativeLayout)findViewById(R.id.speed_indicator); 
     View child = getLayoutInflater().inflate(R.layout.fragment_speed_indicator, null); 
     relativeLayout2.addView(child);