如何绘制一些与你的手指在一个Android应用程序....并将其保存到Web [英] How to Draw something with your finger in an Android App.... And save it to the web

查看:107
本文介绍了如何绘制一些与你的手指在一个Android应用程序....并将其保存到Web的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*的 <一个href="http://wiki.nofolder.com/how-to-draw-something-with-your-finger-in-an-android-app-and-save-it-to-the-web">THIS问题是成功地应答,并已成为一个博客帖子&LT; - 点击 *

*THIS QUESTION WAS SUCCESSFULLY ANSWERED AND HAS BECOME A BLOG POST <- click *

您好我是一个PHP开发人员,我想做一个简单的事情 - 我想画的东西画的空白页上的Andr​​oid手机(用手指有largeish模拟笔尖),并存储该位图作为JPEG服务器通过HTTP POST上。

Hi I am a PHP developer I want to do a simple thing - I want to draw something drawn on a blank page on an Android Phone (with a finger with a largeish "emulated pen nib") and store the bitmap as a jpeg on a server by http post.

下面是我到目前为止,但这是从教程的参与写作精灵的游戏..我不能适应它

Here is what I have so far but this is taken from a tutorial that is involved with writing sprites for a game.. And I cant' adapt it

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>

有人可以帮我吗?我觉得我很接近 - 我用的是蓝色圆球作为笔尖我只是想拯救这个,可能我需要有一个按钮(或菜单)上的XML页面做到这一点?我知道这是一个有点乞讨的,但也有很多的人在网上询问如何绘制你的手指,并保存一些东西到云中,如果人们可能会用code的例子回应(不是引用)我保证,我将这段代码编译成适当的教程一张code所有的最终利益。包括PHP服务器端code,我已经感到很高兴。

Can someone please help me? I feel I am close - I use the blueball as a pen nib I just want to "save" this and possibly I'll need to have a button (or menu) on the XML page to do this ? I know it's a bit of a beg but there are a lot of people online asking how to draw with your finger and save something to the "cloud" if people could respond with examples of the code (not references) I promise I will compile this into a proper tutorial piece of code for the eventual benefit of all. Including the PHP server side code that I already am really happy with.

推荐答案

您可以尝试使用建议谷歌的手势对象,试图执行我的关注code:

You can try to use the Gesture Object proposed by Google, try to execute my following code :

活动1 XML:

<?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>

Java的活动1:

Activity1 java :

 package com.testandroidproject;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Activity1 extends Activity {

    private Button button_save;
    private GestureOverlayView gesture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        gesture = (GestureOverlayView) findViewById(R.id.gestures);
        button_save = (Button) findViewById(R.id.save_button);

        button_save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try {
                    Bitmap gestureImg = gesture.getGesture().toBitmap(100, 100,
                            8, Color.BLACK);

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    gestureImg.compress(Bitmap.CompressFormat.PNG, 100, bos);
                    byte[] bArray = bos.toByteArray();

                    Intent intent = new Intent(Activity1.this, Activity2.class);

                    intent.putExtra("draw", bArray);
                    startActivity(intent);

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(Activity1.this, "No draw on the string",
                            3000).show();
                }
            }
        });
    }

}

活性2 XML:

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>

活性2的java:

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

}

希望你能有所帮助。

Hope you will find this helpful.

这篇关于如何绘制一些与你的手指在一个Android应用程序....并将其保存到Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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