将以下内容保存到名为round_bg.xml
的文件中,放在drawable
文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/white"/>
</shape>
</item>
</selector>
<View
android:layout_width="90dp"
android:layout_height="90dp"
android:background="@drawable/round_bg"/>
所以你可以得到一个圆形。
关于你的第二个问题。你需要研究Recyclerview
和适配器等内容。
rounded_circle.xml
的可绘制资源文件。 <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false">
<solid android:color="@color/colorPrimary" />
<stroke
android:width="1.5dp"
android:color="@color/colorPrimary" />
</shape>
在您的布局XML中使用它:
<View
android:layout_width="@dimen/dimen_20"
android:layout_height="@dimen/dimen_20"
android:background="@drawable/rounded_circle"/>
使用此代码
将其放入名为round_bg.xml的文件中,位于drawable文件夹中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ddd"/>
<corners android:radius="90dp"/>
</shape>
以及您的布局代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<TextView
android:layout_width="275dp"
android:layout_height="match_parent"
android:text="TextView"
android:gravity="center"
android:background="#ddd"
android:layout_marginRight="18dp"
android:layout_marginEnd="18dp" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/circle"/>
</LinearLayout>
Create this file in drawable folder like bg_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="yourcolor"/>
<stroke
android:width="2dp"
android:color="stroke color" />
<size
android:width="100dp"
android:height="100dp"/>
</shape>
你可以使用自定义视图来绘制一个圆形,然后在画布上绘制任何东西。例如:
package com.example.customcircle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
public class CustomCircle extends View {
private Paint paint = null;
public CustomCircle(Context context) {
super(context);
init();
}
public CustomCircle(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomCircle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public CustomCircle(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
paint = new Paint();
paint.setStyle( Paint.Style.FILL );
paint.setColor( Color.BLACK );
}
@Override
protected void onDraw(Canvas canvas) {
int radius = 300;
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint );
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
// handle touch
}
}
使用方法:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<com.example.custumcircle.CustomCircle
android:id="@+id/custumCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我写了一个最简单的画板实现。它是用 Kotlin 编写的。但是当线条数量增加时,它会出现性能问题,并且可以通过许多方式进行优化。
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
class PathDrawView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
// Path is used to describe how the line is drawn by canvas
private val paths = mutableListOf<Path>()
// The path which the user is drawing
private lateinit var current: Path
// init a paint for drawing the lines
private val paint = Paint().apply {
color = Color.BLACK
strokeWidth = 2f
strokeJoin = Paint.Join.ROUND
isDither = true
isAntiAlias = true
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x
val y = event.y
when (event.action) {
MotionEvent.ACTION_DOWN -> { // When use touch down, start the line drawing
current = Path()
paths.add(current)
current.moveTo(x, y)
}
MotionEvent.ACTION_MOVE -> { // Every time user moves, update the path's data
current.lineTo(x, y)
invalidate() // request the view hierarchy to re-draw
}
MotionEvent.ACTION_UP -> {
}
}
return true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
// draw all paths
paths.forEach {
canvas.drawPath(it, paint)
}
}
}
Canvas
类(查看一些教程)来绘制圆形内部,或者也可以尝试使用SignaturePad
SignaturePad on Github。 - Luis Aguilar