android - 在可绘制对象之外绘制背景
问题描述
我必须制作自己的相机预览,但我不知道如何像屏幕其余部分一样填充那些透明的角落。我已经使用 Linears 来绘制表面,但也许还有其他更好的解决方案。
这是我的相机预览布局
<LinearLayout
android:layout_width="match_parent"
android:weightSum="3"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white_transparent"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:weightSum="7"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/white_transparent"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@drawable/barcode_scanner_background"
android:layout_weight="5"
></LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/white_transparent"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white_transparent"
android:layout_weight="1"
/>
</LinearLayout>
这是我的形状背景
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:bottomLeftRadius="25dp" android:bottomRightRadius="25dp" android:topLeftRadius="25dp" android:topRightRadius="25dp" />
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" />
<solid android:color="@android:color/transparent"/>
<stroke android:width="1dip" android:color="@color/green" />
</shape>
</item>
</selector>
提前致谢!
解决方案
最后我解决了它创建我的自定义视图
class CustomView : View {
var paint = Paint()
var path = Path()
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun onDraw(canvas: Canvas) {
paint.strokeWidth = 0f
paint.isAntiAlias = true
paint.style = Paint.Style.FILL_AND_STROKE
paint.color = ContextCompat.getColor(context, R.color.white_transparent)
val frame = RectF(0f, 0f,canvas.width.toFloat(), canvas.height.toFloat())
path.fillType = Path.FillType.INVERSE_EVEN_ODD
path.addRoundRect(frame, 60f, 60f, Path.Direction.CW)
canvas.drawPath(path, paint)
paint.strokeWidth = 3f
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
paint.color = ContextCompat.getColor(context, R.color.green)
val board = RectF(1f, 1f,canvas.width.toFloat()-1f, canvas.height.toFloat()-1f)
canvas.drawRoundRect(board, 55f, 55f, paint)
}
}
而我的布局使用约束布局......
<android.support.constraint.ConstraintLayout 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:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.qrpayment.QRCaptureActivity">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/zxing_barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:zxing_scanner_layout="@layout/custom_barcode_scanner"></com.journeyapps.barcodescanner.DecoratedBarcodeView>
<com.vipera.onepay.ui.component.custom.CustomView
android:id="@+id/customView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:id="@+id/leftView"
android:layout_width="128px"
android:layout_height="0dp"
android:background="@color/white_transparent"
app:layout_constraintBottom_toBottomOf="@+id/customView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/customView"
app:layout_constraintTop_toTopOf="@+id/customView">
</RelativeLayout>
<RelativeLayout
android:id="@+id/rightView"
android:layout_width="128px"
android:layout_height="0dp"
android:background="@color/white_transparent"
app:layout_constraintBottom_toBottomOf="@+id/customView"
app:layout_constraintLeft_toRightOf="@+id/customView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/customView">
</RelativeLayout>
<RelativeLayout
android:id="@+id/topView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white_transparent"
app:layout_constraintBottom_toTopOf="@+id/customView"
app:layout_constraintTop_toTopOf="parent">
</RelativeLayout>
<RelativeLayout
android:id="@+id/bottomView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white_transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customView">
</RelativeLayout>
<ToggleButton
android:id="@+id/toggle"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/toggle_switch"
android:focusable="false"
android:layout_marginStart="-16dp"
app:layout_constraintTop_toBottomOf="@id/customView"
app:layout_constraintLeft_toLeftOf="@id/customView"
android:focusableInTouchMode="false"
android:textOff=""
android:textOn="" />
</android.support.constraint.ConstraintLayout>
有了这个结果:
根据设备,我有一些异常行为
例如,在 Mi8 上,这会从后台返回,而在华为 p8 上,当我收到通知或点击谷歌助手时会发生这种情况。
推荐阅读
- javascript - 图像加载后如何删除加载图标?
- python - How to iterate over multiple lists of different lengths, but repeat the last value of a shorter list until the longest list is done?
- java - Running pitest with maven fails to find or load org.pitest.coverage.execute.CoverageMinion
- amazon-web-services - Provision Concurrency not keeping AWS Lambda function warm and it still has initialization time
- reactjs - ReactJS handling quantiteis in cart
- java - 检查字符串数组中是否存在字符串的有效方法
- javascript - 为什么js最接近的方法找不到兄弟元素?
- android - Flutter Firestore [cloud_firestore/not-found] 找不到一些请求的文档
- angular - 模拟订阅变量
- c# - 如何在 Android 上使用 C# 将文本写入文本文件