2016-10-23 6 views
1

ギャップを持つ円形を作成する必要があります。分割したAndroidのリング形状

クロックを表すプログレスバーを作成しています。各時間に割り当てられた12のギャップ。

enter image description here

これはまさに私が達成したいものです。ここで(デス・スターの外輪)

は私のコードは、これまでのところです:circle_shape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring" 
    android:innerRadiusRatio="2.5" 
    android:thickness="4dp" 
    android:useLevel="false"> 

    <solid android:color="#CCC" /> 
    <stroke 
     android:dashGap="10px" 
     android:dashWidth="10px" 
     android:width="1dp" 
     android:color="#ababb2" /> 
</shape> 
circular_progress_bar

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="270" 
    android:toDegrees="270"> 
    <shape 
     android:innerRadiusRatio="2.5" 
     android:shape="ring" 
     android:thickness="4dp" 
     android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 --> 

     <gradient 
      android:angle="0" 
      android:endColor="#007DD6" 
      android:startColor="#007DD6" 
      android:type="sweep" 
      android:useLevel="false" /> 
    </shape> 
</rotate> 

activity.xml

<ProgressBar 
    android:id="@+id/progressBar" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:indeterminate="false" 
    android:progressDrawable="@drawable/circular_progress_bar" 
    android:background="@drawable/circle_shape" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:max="500" 
    android:progress="65" 
    android:layout_marginBottom="165dp" 
    android:layout_above="@+id/button5" 
    android:layout_centerHorizontal="true" /> 

シェイプをどのように分けると、このように見えますか?私は正しい道にいますか?

答えて

2

過去にカスタムプログレスバーを作成する必要がありました。 あなたの解決策についてはわかりませんので、私はコメントしません。 これは私が問題に近づい方法です:

私はのLinearLayoutをオーバーライドカスタムクラスを作成した(私がいくつかのより多くのビューを追加する必要がありますが、他のビューを上書きすることができます)

その後、私はonDrawオーバーライドして、ちょうどアーチを描きます

ArchProgressBar.java

public class ArchProgressBar extends LinearLayout { 

private static final float START_ANGLE = 130; 
private static final float ARCH_LENGTH = 50; 

public ArchProgressBar(Context context) { 
    super(context); 
    init(context); 
} 

public ArchProgressBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

private void init(Context context) { 
    this.setWillNotDraw(false); 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.arch_progress_bar, this, true); 

    this.postInvalidate(); 
} 

@Override 
public void onDraw(Canvas canvas) { 

    float middleWidth = canvas.getWidth()/2; 
    float middleHeight = canvas.getHeight()/2; 
    float left = middleWidth - 105 * getResources().getDisplayMetrics().density; 
    float top = middleHeight - 90 * getResources().getDisplayMetrics().density; 
    float right = middleWidth + 105 * getResources().getDisplayMetrics().density; 
    float bottom = middleHeight + 120 * getResources().getDisplayMetrics().density; 

    Paint mPaintBackground = new Paint(); 
    mPaintBackground.setAntiAlias(true); 
    mPaintBackground.setStyle(Paint.Style.STROKE); 
    mPaintBackground.setStrokeWidth(35); 
    mPaintBackground.setColor(Color.BLACK); 

    RectF mRectF = new RectF(left, top, right, bottom); 

    // draw background line 
    canvas.drawArc(mRectF, START_ANGLE, ARCH_LENGTH, false, mPaintBackground); 

    canvas.drawArc(mRectF, START_ANGLE + ARCH_LENGTH + 10, ARCH_LENGTH, false, mPaintBackground); 

} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    int width = View.MeasureSpec.getSize(widthMeasureSpec); 
    setMeasuredDimension(width, Math.max(800, heightMeasureSpec)); 
} 

} 

arch_progress_bar.xml

<?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> 
012:キャンバスの上に

、あなたはちょうどあなたがこのようにしたい任意のXMLにそれを追加することができます

<com.training.archprogress.ArchProgressBar 
    android:id="@+id/result_result" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 

あなたはわずか12個の異なるセグメントを描画し、セグメントのサイズ、その後アーチがわずかに小さくすることができます。 次に、進行状況の更新中に必要なセグメントの数を描画します。

私はgithubの中にあなたのために、このソリューションでプロジェクトを置く: https://github.com/nevoroee/ArchProgress

+0

あなたはこれをより手の込んだことができます。何処に行くの? Imは完全にアンドロイドに新しいです。いくつかの小さなプロジェクトを通してそれを学ぶことに決めました。これが私の最初のzooperウィジェットの再作成です。 – CBeTJlu4ok

+0

私は完全なクラスとサンプルアプリケーションのためのgithubリンクでより詳細な説明を追加しました。なぜ誰かが私を落胆させたのか分かりませんでした...しかし、おそらくこの問題のためのより良い解決策があります –

関連する問題