英文:
basic handwriting app issue (not recognition)
问题
以下是您要翻译的部分:
trying to toy around with android studio for a bit, had some inspiration. i tried looking up online and tried ChatGPT but i cant get this to work - what i'm trying to do for now is to show basic handwriting on the screen. the screen is purple colored intentionally so i can see the area im allowed to write in.
im new to this so i cant be too sure about any issues yet. would love if someone could point me in the right direction!
MainActivity.kt:
package com.example.myapplication
import HandWritingView.HandwritingView
import android.content.ContentValues
import android.graphics.Bitmap
import android.os.Bundle
import android.os Environment
import android provider MediaStore
import android.widget Button
import android.widget Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var handwritingView: HandwritingView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
handwritingView = findViewById<HandwritingView>(R.id.handwriting_view)
val saveButton = findViewById<Button>(R.id.save_button)
saveButton.setOnClickListener {
val bitmap = handwritingView?.getBitmap()
// Save the bitmap as a file in the device's external storage
val contentValues = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "handwriting_${System.currentTimeMillis()}.jpg")
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
}
val resolver = applicationContext contentResolver
val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
uri?.let {
resolver openOutputStream(uri).use { outputStream ->
if (bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) == false) {
Toast.makeText(this, "Failed to save image", Toast.LENGTH_SHORT).show()
}
}
Toast.makeText(this, "Image saved successfully", Toast.LENGTH_SHORT).show()
}
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handwrite something and click the 'Save' button to save the image to your device's gallery."
app:layout_constraintBottom_toTopOf="@id/save_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<HandWritingView.HandwritingView
android:id="@+id/handwriting_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/purple_700"
app:layout_constraintBottom_toTopOf="@id/save_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
HandwritingView.kt:
package HandWritingView
import android.content Context
import android.graphics.*
import android.util AttributeSet
import android.view MotionEvent
import android.view View
class HandwritingView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val paint = Paint().apply {
color = Color.BLACK
style = Paint.Style.STROKE
strokeWidth = 12f
isAntiAlias = true
isDither = true
}
private var path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
private var canvas = Canvas(path)
private var lastX = 0f
private var lastY = 0f
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas drawBitmap(path, null, RectF(0f, 0f, width.toFloat(), height.toFloat()), null)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x
val y = event.y
when (event.action) {
MotionEvent.ACTION_DOWN -> {
lastX = x
lastY = y
performClick() // Call performClick on ACTION_DOWN event
return true
}
MotionEvent.ACTION_MOVE -> {
canvas.drawLine(lastX, lastY, x, y, paint)
lastX = x
lastY = y
invalidate()
return true
}
MotionEvent.ACTION_UP -> {
canvas.drawLine(lastX, lastY, x, y, paint)
invalidate()
return true
}
}
return super.onTouchEvent(event)
}
override fun performClick(): Boolean {
super.performClick()
// Your code here, if any
return true
}
fun clear() {
path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
canvas = Canvas(path)
invalidate()
}
fun getBitmap(): Bitmap {
return path
}
}
希望这能帮助您解决问题。如果您有任何其他问题,请随时提问。
英文:
trying to toy around with android studio for a bit, had some inspiration. i tried looking up online and tried ChatGPT but i cant get this to work - what i'm trying to do for now is to show basic handwriting on the screen. the screen is purple colored intentionally so i can see the area im allowed to write in.
im new to this so i cant be too sure about any issues yet. would love if someone could point me in the right direction!
MainActivity.kt:
package com.example.myapplication
import HandWritingView.HandwritingView
import android.content.ContentValues
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var handwritingView: HandwritingView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
handwritingView = findViewById<HandwritingView>(R.id.handwriting_view)
val saveButton = findViewById<Button>(R.id.save_button)
saveButton.setOnClickListener {
val bitmap = handwritingView?.getBitmap()
// Save the bitmap as a file in the device's external storage
val contentValues = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, "handwriting_${System.currentTimeMillis()}.jpg")
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
}
val resolver = applicationContext.contentResolver
val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
uri?.let {
resolver.openOutputStream(uri).use { outputStream ->
if (bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) == false) {
Toast.makeText(this, "Failed to save image", Toast.LENGTH_SHORT).show()
}
}
Toast.makeText(this, "Image saved successfully", Toast.LENGTH_SHORT).show()
}
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handwrite something and click the 'Save' button to save the image to your device's gallery."
app:layout_constraintBottom_toTopOf="@id/save_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<HandWritingView.HandwritingView
android:id="@+id/handwriting_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/purple_700"
app:layout_constraintBottom_toTopOf="@id/save_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
HandwritingView.kt:
package HandWritingView
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
class HandwritingView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val paint = Paint().apply {
color = Color.BLACK
style = Paint.Style.STROKE
strokeWidth = 12f
isAntiAlias = true
isDither = true
}
private var path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
private var canvas = Canvas(path)
private var lastX = 0f
private var lastY = 0f
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawBitmap(path, null, RectF(0f, 0f, width.toFloat(), height.toFloat()), null)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x
val y = event.y
when (event.action) {
MotionEvent.ACTION_DOWN -> {
lastX = x
lastY = y
performClick() // Call performClick on ACTION_DOWN event
return true
}
MotionEvent.ACTION_MOVE -> {
canvas.drawLine(lastX, lastY, x, y, paint)
lastX = x
lastY = y
invalidate()
return true
}
MotionEvent.ACTION_UP -> {
canvas.drawLine(lastX, lastY, x, y, paint)
invalidate()
return true
}
}
return super.onTouchEvent(event)
}
override fun performClick(): Boolean {
super.performClick()
// Your code here, if any
return true
}
fun clear() {
path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
canvas = Canvas(path)
invalidate()
}
fun getBitmap(): Bitmap {
return path
}
}
like i said im new to this topic so please dont be too harsh on me if it's inappropriate in anyway. i was lost trying to learn and thought praticing this way is the best way. really sorry if this is the case!
I'm adding my file layout, just in case:
all i was trying to do is a simple handwriting app and show in black the handwriting.
on the emulator i don't even see the purple area at all.
on my physical device(s) i do see it, but cant see any handwriting done, by finger or pen.
im not sure how come those are different (guessing android version? maybe?) and why the handwriting isn't shown.
any help would be much appreciated!!! ty
答案1
得分: 0
你的视图还算接近,但也不完全正确。使用一个Path对象。使用lineTo将所有移动的触摸事件累积到Path中。在onDraw中绘制这个路径。完全不需要Bitmap。
英文:
Your View isn't too far off, but it isn't quite right either. Use a Path object. Accumulate all the move touch events into the Path using lineTo. Draw the path in onDraw. No need to have a Bitmap at all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论