10

**この質問は正常に回答され、ブログ投稿になりました<-クリック**

こんにちは私はPHP開発者です簡単なことをしたいです-Android携帯の空白のページに描かれたものを(大きな「エミュレートされたペン先」の指で)描き、ビットマップをjpegとしてhttppostによるサーバー。

これが私がこれまでに持っているものですが、これはゲームのスプライトを書くことに関係するチュートリアルから取られています。そして私はそれを適応させることができません

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サーバー側のコードを含みます。

4

2 に答える 2

10

Google が提案した Gesture Object を使用してみてください。次のコードを実行してみてください。

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

Activity1 Java :

パッケージcom.testandroidproject;

java.io.ByteArrayOutputStream をインポートします。

android.app.Activity をインポートします。
android.content.Intent をインポートします。
android.gesture.GestureOverlayView をインポートします。
android.graphics.Bitmap をインポートします。
android.graphics.Color をインポートします。
android.os.Bundle をインポートします。
android.view.View をインポートします。
android.view.View.OnClickListener をインポートします。
android.widget.Button をインポートします。
android.widget.Toast をインポートします。

public class Activity1 extends Activity {

    プライベート ボタン button_save;
    プライベート GestureOverlayView ジェスチャ;

    @オーバーライド
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        ジェスチャ = (GestureOverlayView) findViewById(R.id.gestures);
        button_save = (ボタン) findViewById(R.id.save_button);

        button_save.setOnClickListener(new OnClickListener() {

            @オーバーライド
            public void onClick(View arg0) {
                試す {
                    ビットマップ ジェスチャImg = ジェスチャ.getGesture().toBitmap(100, 100,
                            8、カラー。ブラック);

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

                    インテント インテント = 新しいインテント(Activity1.this, Activity2.class);

                    Intent.putExtra("draw", bArray);
                    startActivity(意図);

                キャッチ(例外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));
    }

}

これが役立つことを願っています。

于 2012-07-24T13:57:48.613 に答える
2

「保存」のどの部分を達成しようとしているのかはわかりませんが、キャンバスに描いたものをビットマップに保存する方法を尋ねていると思います。

まず、描画するビットマップを作成します。たとえば、canvasBitmap. それで:

c.setBitmap(canvasBitmap);

これにより、「canvasBitmap」に描画されたすべてが格納されます。次に、ユーザーがボタンを押して保存すると、次のようになります。

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

次に、savedBitmap を取得してクラウドに送信します。それが役立つことを願っています。

于 2012-07-24T13:37:31.670 に答える