Android绘图半圆与画布

时间:2016-02-01 16:14:14

标签: android

任何人都可以帮我解决如何使用画布绘制下面图片的半圆以及如何检测绘图对象的集合吗?

Drawing Object

我曾尝试使用XML绘制它,我不知道如何检测它的碰撞。我只是想检测黑色部分的碰撞而不是整个圆圈的碰撞。 谢谢。

2 个答案:

答案 0 :(得分:0)

  1. 在onDraw方法中绘制一个圆圈。

    canvas.drawCircle(x,y,10,paint);

  2. 现在绘制一个带有背景颜色的矩形

    void CDatabaseMonitor::ResumeAllThreads() 
    {
        DWORD dwCurProc = GetCurrentProcessId();
        HANDLE hCurProc = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    
        if (hCurProc != INVALID_HANDLE_VALUE)
        {
            THREADENTRY32 te = {0};
            te.dwSize = sizeof(te);
    
            if (Thread32First(hCurProc, &te))
            {
                do
                {
                    if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID))
                    {
                        if (te.th32ThreadID != m_currentThreadId && te.th32OwnerProcessID == dwCurProc)
                        {
                            HANDLE thread = ::OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
    
                            if (thread != NULL)
                            {
                                ResumeThread(thread);
                                CloseHandle(thread);
                            }
                        }
                    }
    
                    te.dwSize = sizeof(te);
                }
                while (Thread32Next(hCurProc, &te));
            }
    
            CloseHandle(hCurProc);
        }
    }
    
  3. 这应该有助于达到目的。

答案 1 :(得分:0)

我刚刚使用自定义视图执行了类似的操作:

class SemiCircleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint: Paint = Paint()
    private var rectangle: RectF? = null
    private var margin: Float

    init {
        paint.isAntiAlias = true
        paint.color = ContextCompat.getColor(context, R.color.colorAquamarine)
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5.dpToPx()
        margin = 3.dpToPx() // margin should be >= strokeWidth / 2 (otherwise the arc is cut)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (rectangle == null) {
            rectangle = RectF(0f + margin, 0f + margin, width.toFloat() - margin, height.toFloat() - margin)
        }
        canvas.drawArc(rectangle!!, 90f, 180f, false, paint)
    }

}

像这样将其添加到您的XML布局中:

<com.example.view.SemiCircleView
    android:layout_width="120dp"
    android:layout_height="120dp"/>

这就是结果:

Android semi circle canvas setArc


就我而言,我想以编程方式控制要绘制的圆的百分比,因此,我添加了一种方法setArcProportion来控制:

class SemiCircleView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val mainPaint: Paint = Paint()
    private val backgroundPaint: Paint = Paint()
    private var rectangle: RectF? = null
    private var margin: Float
    private var arcProportion: Float = 0f

    init {
        mainPaint.isAntiAlias = true
        mainPaint.color = ContextCompat.getColor(context, R.color.colorLutea)
        mainPaint.style = Paint.Style.STROKE
        mainPaint.strokeWidth = 5.dpToPx()
        backgroundPaint.isAntiAlias = true
        backgroundPaint.color = ContextCompat.getColor(context, R.color.black_08)
        backgroundPaint.style = Paint.Style.STROKE
        backgroundPaint.strokeWidth = 5.dpToPx()
        margin = 3.dpToPx() // margin should be >= strokeWidth / 2 (otherwise the arc is cut)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (rectangle == null) {
            rectangle = RectF(0f + margin, 0f + margin, width.toFloat() - margin, height.toFloat() - margin)
        }
        canvas.drawArc(rectangle!!, -90f, arcProportion * 360, false, mainPaint)
        // This 2nd arc completes the circle. Remove it if you don't want it
        canvas.drawArc(rectangle!!, -90f + arcProportion * 360, (1 - arcProportion) * 360, false, backgroundPaint)
    }

    /**
     * @param arcProportion The proportion of the semi circle arc, from 0 to 1. Setting 0 makes the arc invisible, and 1
     * makes a whole circle.
     */
    fun setArcProportion(arcProportion: Float) {
        this.arcProportion = arcProportion
        invalidate()
    }

}

因此,如果我这样做semiCircleView.setArcProportion(0.62f),我将拥有:

Android semi circle canvas setArc

奖金-要使弧随着动画而增长,请像这样修改setArcProportion

private const val ANIMATION_BASE_DURATION_MS: Long = 500 // milliseconds

fun setArcProportion(arcProportion: Float) {
    ValueAnimator.ofFloat(0f, arcProportion).apply {
        interpolator = DecelerateInterpolator()
        // The animation duration is longer for a larger arc
        duration = ANIMATION_BASE_DURATION_MS + (arcProportion * ANIMATION_BASE_DURATION_MS).toLong()
        addUpdateListener { animator ->
            this@SemiCircleView.arcProportion = animator.animatedValue as Float
            this@SemiCircleView.invalidate()
        }
        start()
    }
}