Android 사용자 정의 점선 그리 기 컨트롤

안 드 로 이 드 의 UI 개발 에 서 는 인터페이스 미관 을 위해 점선 을 사용 해 야 할 때 도 있다.여기 서 나 는 스스로 점선 을 그 리 는 컨트롤 을 실현 한다.
package com.custom;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
 
public class DashedLine extends View {
    private final String namespace = "http://www.android-study.com/";
    private float startX;
    private float startY;
    private float endX;
    private float endY;
    private Rect mRect;
 
    public DashedLine(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.DKGRAY);
        Path path = new Path();
        path.moveTo(0, 10);
        path.lineTo(480, 10);
        PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
        paint.setPathEffect(effects);
        canvas.drawPath(path, paint);
    }
}

layot 폴 더 의 xml 에서 이 컨트롤 을 참조 합 니 다.
<com.custom.DashedLine
    android:id="@+id/dashedLine"
    android:layout_width="wrap_content"
    android:layout_height="20px"
    />

좋은 웹페이지 즐겨찾기