如何保存画布绘图,并在按钮被点击时显示 [英] How to save canvas drawing and display it when the button is clicked

查看:242
本文介绍了如何保存画布绘图,并在按钮被点击时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建Android应用程序,我可以在画布上用底部的两个按钮
i管理,以使绘图工作,但现在我需要添加两个按钮(一个是用于保存绘图canvas到数据库(Sqlite如果可能)或位图和另一个从sqlite导入图像并显示它到graphicview)



现在我不知道什么代码我必须放入它使保存和显示按钮的工作,所以请帮助我的代码,谢谢你的时间:)



这里是我的代码(图形绘制工作正常,现在我需要保存和显示按钮的代码以及在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){
//绘图命令到这里

绘制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(你点击了+ downx +,+ downy,200,600,
textPaint);
for(int i = 0; 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;
默认值:
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));
}

main.xml
$ b

 <?xml version =1.0encoding =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 Pictureandroid: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);

//现在位图有你的画布图片

//设置为ImageView
yourImageView.setImageBitmap(bitmap);

//或保存到sdcard
文件dir = new File(/ sdcard / yourAppFolder /);
if(!dir.isDirectory()){
dir.mkdirs();
}
文件outputFile = new File(dir,image.jpg);
OutputStream fout = null;
fout = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fout);
fout.flush();
fout.close();


hey i am trying to create android app where i can draw on canvas with two buttons on the bottom i managed to get the drawing to work however right now i need to add two buttons(one is for saving the drawing canvas into database (Sqlite if possible) or bitmap and the other one to import the image from the sqlite and display it into the graphicview)

right now i have no idea what code i have to put into it to make the save and display button works so please help me with the code and thank you for your time :)

here is my code at the moment (the graphic drawing works fine and now i need the code for the save and display button as well as what ive missed in the graphicview.java and 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));
}

main.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>

My AndroidManifest.xml is still at default because i havent edit anything in it yet

解决方案

You can use this code in your save button click listener.

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();

这篇关于如何保存画布绘图,并在按钮被点击时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆