我已经为EditText创建了Shape。而且,我要做的只是为它蒙上阴影。我尝试了一切,但阴影没有来临。
以下是我的形状
我尝试设置android:elevation="6dp"
和android:translationZ="2dp"
,但没有成功。
这是我的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorGreen"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:clipToPadding="false"
android:elevation="6dp"
android:gravity="center"
android:translationZ="2dp">
<EditText
android:id="@+id/left"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:elevation="20dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingEnd="10dp" />
<com.brar.shape.LeftShape
android:id="@+id/myshape"
android:layout_width="130dp"
android:layout_height="60dp"
android:elevation="2dp"
app:fillColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
我的自定义视图代码如下:
import android.content.Context
import android.content.res.TypedArray
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
class LeftShape @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val attribute = attrs
private val paintArc: Paint = Paint()
private val paintHorizontalLine: Paint = Paint()
private var color: Int
val path = Path();
init {
val ta: TypedArray =
context.obtainStyledAttributes(attribute, R.styleable.LeftShapeView, 0, 0)
color = Color.WHITE;
try {
color = ta.getColor(com.brar.shape.R.styleable.LeftShapeView_fillColor, Color.WHITE);
} finally {
ta.recycle();
}
paintArc.isAntiAlias = true
paintArc.color = color
paintArc.style = Paint.Style.FILL
paintHorizontalLine.isAntiAlias = true
paintHorizontalLine.color = color
paintHorizontalLine.style = Paint.Style.STROKE
paintHorizontalLine.strokeWidth = 8f
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
var width = canvas!!.width
var height = canvas!!.height
if (width == 0) width = (height * 2)
val radius = height / 2
val circleTop = 0;
val circleLeft = 0;
val circleBottom = radius
val circleRight = radius
val fromAngle = 90f;
val sweepAngle = 180f;
val rectF = RectF(
circleLeft.toFloat(),
circleTop.toFloat(),
circleRight.toFloat(),
circleBottom.toFloat()
)
canvas!!.drawArc(rectF, fromAngle, sweepAngle, false, paintArc)
//Draw Rectangle and Right angle triangle
val rectangleTopEnd = width;
val topTopStart = radius / 2;
var lastWidthOfRectangle = width;
Log.d("AK", "Rectangle Width: $width")
if (width > 0) {
val widthToReduce = width.toFloat() * 0.10
Log.d("AK", "Rectangle widthToReduce: ${widthToReduce}")
if (width > widthToReduce) {
lastWidthOfRectangle = (width - widthToReduce).toInt()
Log.d("AK", "Rectangle lastWidthOfRectangle: ${lastWidthOfRectangle}")
} else {
Log.d("AK", "What happened")
}
}
path.moveTo(rectangleTopEnd.toFloat(), 0f)
path.lineTo(rectangleTopEnd.toFloat(), 0f)
path.lineTo(topTopStart.toFloat(), 0f)
path.lineTo(topTopStart.toFloat(), radius.toFloat())
path.lineTo(lastWidthOfRectangle.toFloat(), radius.toFloat())
path.lineTo(width.toFloat(), 0f)
canvas.drawPath(path, paintArc);
}
}