gpt4 book ai didi

android - 如何保存 Canvas 绘图并在单击按钮时显示它

转载 作者:行者123 更新时间:2023-11-29 01:37:54 24 4
gpt4 key购买 nike

嘿,我正在尝试创建一个安卓应用程序,我可以在 Canvas 上绘图,底部有两个按钮我设法让绘图工作但是现在我需要添加两个按钮(一个用于将绘图 Canvas 保存到数据库(如果可能的话是 Sqlite)或位图,另一个用于从 sqlite 导入图像并将其显示到图形 View )

现在我不知道我必须在其中输入什么代码才能使保存和显示按钮正常工作,所以请帮助我处理代码并感谢您抽出时间:)

这是我目前的代码(绘图工作正常,现在我需要保存和显示按钮的代码以及我在 graphicview.java 和 MainActivity.java 中遗漏的代码)

GraphicsView.java

package org.example.graphics;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Path.Direction;
import android.view.*;

public class GraphicsView extends View implements View.OnTouchListener {

public GraphicsView(Context context) {
super(context);
setBackgroundColor(Color.WHITE);
setOnTouchListener(this);
}

ArrayList<MyPoint> arrOfPoints=new ArrayList<MyPoint>();

class MyPoint
{
float x, y;
}
float downx, downy;

@Override
protected void onDraw(Canvas canvas) {
// Drawing commands go here

Paint rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
rectPaint.setStyle(Paint.Style.FILL_AND_STROKE);
rectPaint.setColor(Color.BLACK);
canvas.drawRoundRect(new RectF(0, 0, 100, 100), 10, 10, rectPaint);

rectPaint.setColor(Color.BLUE);
rectPaint.setAlpha(40);
canvas.drawCircle(300, 300, 200, rectPaint);
canvas.drawRect(new RectF(40, 40, 200, 200), rectPaint);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG
| Paint.STRIKE_THRU_TEXT_FLAG);
canvas.drawText("Hello there!", 200, 200, textPaint);

canvas.drawText("You clicked on " + downx + "," + downy, 200, 600,
textPaint);
for(int i=0;i<arrOfPoints.size();i++)
{
canvas.drawCircle(arrOfPoints.get(i).x, arrOfPoints.get(i).y, 20, rectPaint);
}

}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
int action = arg1.getAction();
MyPoint p=null;
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = arg1.getX();
downy = arg1.getY();

p=new MyPoint();
p.x=arg1.getX();p.y=arg1.getY();

arrOfPoints.add(p);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
downx = arg1.getX();
downy = arg1.getY();
p=new MyPoint();
p.x=arg1.getX();p.y=arg1.getY();
arrOfPoints.add(p);
invalidate();
break;
case MotionEvent.ACTION_UP:

break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}

MainActivity.java

package org.example.graphics;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsView(this));
}

ma​​in.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">

<org.example.graphics.GraphicsView
android:id="@+id/graphics"
android:layout_width="fill_parent"
android:layout_height="341dp" />

<Button
android:id="@+id/ChoosePictureButton"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose Picture"

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Picture" android:id="@+id/SavePictureButton"/>

</LinearLayout>

我的 AndroidManifest.xml 仍处于默认状态,因为我还没有编辑其中的任何内容

最佳答案

您可以在保存按钮点击监听器中使用此代码。

int canvasWidth = 500;
int canvasHeight = 500;
View v = new GraphicsView(MainActivity.this);
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);

//now bitmap has your canvas image

//set it to your ImageView
yourImageView.setImageBitmap(bitmap);

//or save to sdcard
File dir = new File("/sdcard/yourAppFolder/");
if (!dir.isDirectory()) {
dir.mkdirs();
}
File outputFile = new File(dir, "image.jpg");
OutputStream fout = null;
fout = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();

关于android - 如何保存 Canvas 绘图并在单击按钮时显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com