如何在Android应用程序中用手指绘制东西..并将其保存到网络上



**此问题已成功回答,并已成为博客帖子<-点击**

嗨,我是一名PHP开发人员,我想做一件简单的事——我想在安卓手机上的空白页面上画一些东西(用手指和一个大的"模拟笔尖"),并通过http post将位图作为jpeg存储在服务器上。

这是我到目前为止所做的,但这是从一个为游戏编写精灵的教程中截取的。。我不能适应

package com.my.example;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class DrawCapture extends Activity implements OnTouchListener{
    OurView v;
    Bitmap ball;
    float x,y;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_capture);
        v = new OurView(this);
        v.setOnTouchListener(this);
        ball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
        x = y = 0;
        setContentView(v);
    }
    @Override
    protected void onPause(){
        super.onPause();
        v.pause();
    }
    protected void onResume(){
        super.onResume();
        v.resume();
    }
    public class OurView extends SurfaceView implements Runnable{
        Thread t = null;
        SurfaceHolder holder;
        boolean isItOK = false;
        public OurView(Context context) {
            super(context);
            holder = getHolder();
        }
        public void run() {
            while (isItOK == true){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //perform canvas drawing
                if (!holder.getSurface().isValid()){
                    continue;
                }
                Canvas c = holder.lockCanvas();
                onDraw(c);
                holder.unlockCanvasAndPost(c);
            }       
        }
        public void onDraw(Canvas c){
            c.drawARGB(255, 210, 210, 210);
            c.drawBitmap(ball, x - (ball.getWidth()/2), y - (ball.getHeight()/2), null);
        }
        public void pause(){
            isItOK = false;
            while(true){
                try{
                    t.join();
                } catch(InterruptedException e){
                    e.printStackTrace();
                }
                break;
            }
            t = null;
        }
        public void resume(){
            isItOK = true;
            t = new Thread(this);
            t.start();
        }
    }
    public boolean onTouch(View v, MotionEvent me){
        switch (me.getAction()){
            case MotionEvent.ACTION_DOWN : 
                x = me.getX();
                y = me.getY();              
                break;
            case MotionEvent.ACTION_UP : 
                x = me.getX();
                y = me.getY();              
                break;
            case MotionEvent.ACTION_MOVE : 
                x = me.getX();
                y = me.getY();              
                break;
        }
        return true;
    }
}

这是XML

<?xml version="1.0" encoding="utf-8"?>
<SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</SurfaceView>

有人能帮帮我吗?我觉得我很接近了——我用blueball作为笔尖——我只想"保存"这个,可能我需要在XML页面上有一个按钮(或菜单)才能做到这一点?我知道这有点乞求,但网上有很多人问,如果人们能用代码示例(而不是引用)来回应,如何用手指画画并将东西保存到"云端",我保证我会把它编译成一段合适的教程代码,最终造福所有人。包括我已经非常满意的PHP服务器端代码。

你可以尝试使用谷歌提出的手势对象,尝试执行我的以下代码:

Activity1xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF" >
        <android.gesture.GestureOverlayView
            android:id="@+id/gestures"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fadeEnabled="false"
            android:fadeOffset="5000000000"
            android:gestureColor="#000000"
            android:gestureStrokeType="multiple"
            android:gestureStrokeWidth="1"
            android:uncertainGestureColor="#000000"
            android:layout_above="@+id/save_button" />
        <Button
            android:id="@id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20sp"
            android:paddingLeft="20sp"
            android:paddingRight="20sp"
            android:text="Save"
            android:textSize="22sp" />
    </RelativeLayout>

Activity1 java:

包com.testandroidproject;导入java.io.ByteArrayOutputStream;导入android.app.Activity;导入android.content.Intent;importandroid.gesture.GestureOverlayView;import android.graphics.Bitmap;导入android.graphics.Color;导入android.os.Bundle;导入android.view.view;导入android.view.view.OnClickListener;导入android.widget.Button;导入android.widget.Tast;公共类Activity1扩展Activity{私有按钮按钮保存;private手势OverlayView手势;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);手势=(手势OverlayView)findViewById(R.id.gestures);button_save=(button)findViewById(R.id.save_button);button_save.setOnClickListener(新的OnClickListenr(){@Overridepublic void onClick(视图arg0){尝试{Bitmap gestureImg=手势.getGesture().to位图(100,8,颜色。黑色);ByteArrayOutputStream bos=新的ByteArrayOutput Stream();手势图像压缩(Bitmap.CompressFormat.PNG,100,bos);byte[]bArray=bos.toByteArray();Intent Intent=new Intent(Activity1.this,Activity2.class);intent.putExtra("draw",b数组);startActivity(intent);}catch(异常e){e.printStackTrace();Toast.makeText(Activity1.this,"字符串上没有绘制",3000).show();}}});}}

Activity2 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/image_saved"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

Activity2 java:

package com.testandroidproject;
import java.io.ByteArrayInputStream;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class Activity2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_image);
        ImageView image = (ImageView) findViewById(R.id.image_saved);
        ByteArrayInputStream imageStreamClient = new ByteArrayInputStream(
                getIntent().getExtras().getByteArray("draw"));
        image.setImageBitmap(BitmapFactory.decodeStream(imageStreamClient));
    }
}

希望你会觉得这很有帮助。

我不确定您试图完成"保存"的哪一部分,但我假设您在询问如何将画布上绘制的内容存储为位图。

首先,为自己制作一个位图。比如,canvasBitmap。然后:

c.setBitmap(canvasBitmap);

这将存储已绘制到"canvasBitmap"中的所有内容然后,当用户按下按钮保存时:

savedBitmap = Bitmap.copy(canvasBitmap.getConfig(), true);

现在,获取savedBitmap并将其发射到云中。希望能有所帮助。

最新更新