Java 在远程图像上绘制(用手指)并保存。(安卓)

Java 在远程图像上绘制(用手指)并保存。(安卓),java,android,Java,Android,我目前正在从一个目录加载远程图像列表。我希望用户能够选择一个,然后用手指画出图像,然后保存它。(最终,用修改后的映像替换目录中的现有映像) 我想用这个给我一些正确的指示 谢谢 首先研究graphics文件夹下示例中的fingerpaint.java 您应该覆盖视图的onDraw方法 screen\u drawing\u room.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=

我目前正在从一个目录加载远程图像列表。我希望用户能够选择一个,然后用手指画出图像,然后保存它。(最终,用修改后的映像替换目录中的现有映像)

我想用这个给我一些正确的指示


谢谢

首先研究graphics文件夹下示例中的fingerpaint.java

您应该覆盖视图的onDraw方法

screen\u drawing\u room.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rlid"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 <LinearLayout   //set background for the bottom layout set image here.
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom"
    android:orientation="vertical"
    android:weightSum="1.0" >

    <LinearLayout
        android:id="@+id/view_drawing_pad"//your drawing pad on foreground
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </LinearLayout>
</LinearLayout>
</RelativeLayout
DrawingView.java

定义您的DrawingView。下图可用于手绘。对其进行修改以绘制线条、文本和填充颜色(闭合区域)

应添加颜色选择器并将颜色设置为绘制对象,以允许用户选择绘制颜色

有关洪水填充,请参见链接中的接受答案

已设置工程视图。从图形文件夹下的sdk UMSample中学习fingerpaint.java

由此产生的桑普肖特。你在背景中看到的是一幅图像。在前景我画你好。我在拐角处画了线。如果你能辨认出它是红色的

画一条看起来像边框的线。将绘制的笔划宽度设置为任意值。类似地,可以通过更改x1、y1和x2、y2坐标在需要的位置绘制直线

                Display display = ( (Activity) mcontext).getWindowManager().getDefaultDisplay();  
                float w = display.getWidth(); 
                float h = display.getHeight();
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                canvas.drawLine(0, 0, w, 0,mBitmapPaint);
                canvas.drawLine(0, 0, 0, h,mBitmapPaint);
                canvas.drawLine(w,h,w,0,mBitmapPaint);
                canvas.drawLine(w, h, 0,h , mBitmapPaint);

挽救

 AlertDialog.Builder editalert = new AlertDialog.Builder(DrawingRoomScreen.this);
 editalert.setTitle("Please Enter the name with which you want to Save");
 final EditText input = new EditText(DrawingRoomScreen.this);       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.FILL_PARENT);
                input.setLayoutParams(lp);
                editalert.setView(input);
                editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        content.setDrawingCacheEnabled(true);
                        String name= input.getText().toString();
                        Bitmap bitmap = mDrawingPad.getDrawingCache();//root view

                     String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
                        File file = new File("/sdcard/"+name+".png");           
                        try 
                        {
                            if(!file.exists())
                        {
                            file.createNewFile();
                        }
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(CompressFormat.PNG, 10, ostream);
                            System.out.println("saving......................................................"+path);
                            ostream.close();
                            //content.invalidate();                         
                        } 
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }finally
                        {


                        }
                    }
                });

                editalert.show();       
从图库中获取图像

  File fp;
  Drawable d;   

public void  setImagefrmGallery()
{
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
// To handle when an image is selected from the browser, add the following to your Activity 
 }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// currImageURI is the global variable I�m using to hold the content:// URI of the image
Uri currImageURI = data.getData();
System.out.println("Hello======="+getRealPathFromURI(currImageURI));
String s= getRealPathFromURI(currImageURI);
File file = new File(s);

    if (file.exists()) {
     fp=file.getAbsolutePath();
     d = Drawable.createFromPath(file.getAbsolutePath());
     mDrawingPad.setBackgroundDrawable(d);
   }
   else
   {
    System.out.println("File Not Found");
    }
}
}
}
// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
// can post image
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index); 
   }
 AlertDialog.Builder editalert = new AlertDialog.Builder(DrawingRoomScreen.this);
 editalert.setTitle("Please Enter the name with which you want to Save");
 final EditText input = new EditText(DrawingRoomScreen.this);       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.FILL_PARENT);
                input.setLayoutParams(lp);
                editalert.setView(input);
                editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        content.setDrawingCacheEnabled(true);
                        String name= input.getText().toString();
                        Bitmap bitmap = mDrawingPad.getDrawingCache();//root view

                     String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
                        File file = new File("/sdcard/"+name+".png");           
                        try 
                        {
                            if(!file.exists())
                        {
                            file.createNewFile();
                        }
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(CompressFormat.PNG, 10, ostream);
                            System.out.println("saving......................................................"+path);
                            ostream.close();
                            //content.invalidate();                         
                        } 
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }finally
                        {


                        }
                    }
                });

                editalert.show();       
  File fp;
  Drawable d;   

public void  setImagefrmGallery()
{
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
// To handle when an image is selected from the browser, add the following to your Activity 
 }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// currImageURI is the global variable I�m using to hold the content:// URI of the image
Uri currImageURI = data.getData();
System.out.println("Hello======="+getRealPathFromURI(currImageURI));
String s= getRealPathFromURI(currImageURI);
File file = new File(s);

    if (file.exists()) {
     fp=file.getAbsolutePath();
     d = Drawable.createFromPath(file.getAbsolutePath());
     mDrawingPad.setBackgroundDrawable(d);
   }
   else
   {
    System.out.println("File Not Found");
    }
}
}
}
// And to convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
// can post image
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index); 
   }