How to Draw something with your finger in an Android App… And save it to the web

后端 未结 2 722
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 23:51

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

Hi I am a PHP developer I want to do a simple thing - I want to draw something drawn

2条回答
  •  不知归路
    2021-02-06 00:25

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

    Activity1 xml :

    
        
    
            
    
            

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

    Activity2 xml :

    
    
    
        
    
    
    

    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.

提交回复
热议问题