Android 初学者想要创造阻力&;投递申请

Android 初学者想要创造阻力&;投递申请,android,android-studio,Android,Android Studio,我是Android开发新手,但熟悉编程。我已经学习了一些基础知识,例如片段、OnClickListeners等。作为我的第一个android项目,我想创建一个应用程序,其中 我可以将形状拖放到背景图像上(下图) 将图像另存为JPEG格式并保存到手机 有人能告诉我实现这一点需要了解哪些概念和小部件,并为我提供合适的教程吗?最好是视频 给你。 1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <GridLayout

我是Android开发新手,但熟悉编程。我已经学习了一些基础知识,例如片段、OnClickListeners等。作为我的第一个android项目,我想创建一个应用程序,其中

  • 我可以将形状拖放到背景图像上(下图)
  • 将图像另存为JPEG格式并保存到手机
  • 有人能告诉我实现这一点需要了解哪些概念和小部件,并为我提供合适的教程吗?最好是视频

    给你。 1.activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:id="@+id/toplinear"
            android:layout_width="fill_parent"
            android:layout_height="170dp"
            android:layout_marginBottom="10dp" >
    
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/a" />
    
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/bottomlinear"
            android:layout_width="fill_parent"
            android:layout_height="170dp"
             >
    
            <TextView
                android:id="@+id/text"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="Drag the image and drop it here..." />
    
        </LinearLayout>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:id="@+id/save"
            android:text="Save as file"/>
    
    </GridLayout>
    
    5.AndroidManifest.xml添加以下权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    
    
    6.输出将如下所示


    。这就是您要找的吗?您好,这真的很好,但我希望找到一个解释所有这些代码的教程。如果这个示例也解决了您的问题,您能接受答案吗?
    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <stroke
            android:width="2dp"
            android:color="#FFFF0000" />
    
        <gradient
            android:angle="225"
            android:endColor="#DD2ECCFA"
            android:startColor="#DD000000" />
    
        <corners
            android:bottomLeftRadius="7dp"
            android:bottomRightRadius="7dp"
            android:topLeftRadius="7dp"
            android:topRightRadius="7dp" />
    
    </shape>
    
    public class MainActivity extends AppCompatActivity {
    
        private ImageView myImage;
        private static final String IMAGEVIEW_TAG = "The Android Logo";
        Button btn;
        File file,f;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
            myImage = (ImageView)findViewById(R.id.image);
            // Sets the tag
            myImage.setTag(IMAGEVIEW_TAG);
            btn = (Button)findViewById(R.id.save);
    
            // set the listener to the dragging data
            myImage.setOnTouchListener(new MyClickListener());
    
            findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
            findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LinearLayout content = (LinearLayout)findViewById(R.id.bottomlinear);
                    content.setDrawingCacheEnabled(true);
                    Bitmap bitmap = content.getDrawingCache();
    
    
                    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                    {
                        file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
                        if(!file.exists())
                        {
                            file.mkdirs();
    
                        }
                        f = new File(file.getAbsolutePath()+file.separator+ "filename"+".png");
                        Toast.makeText(getApplicationContext(),"File saved successfully",Toast.LENGTH_SHORT).show();
                    }
    
                    try {
                        FileOutputStream ostream = new FileOutputStream(f);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
                        ostream.close();
                    }catch (Exception e){
                        e.printStackTrace();
                        //Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
                    }
                }
            });
    
        }
    
        private final class MyClickListener implements View.OnTouchListener {
    
    
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
    
                String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
                ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    
                view.startDrag( data, //data to be dragged
                        shadowBuilder, //drag shadow
                        view, //local data about the drag and drop operation
                        0   //no needed flags
                );
    
    
                view.setVisibility(View.INVISIBLE);
                return true;
            }
    
        }
    
        class MyDragListener implements View.OnDragListener {
            Drawable normalShape = getResources().getDrawable(R.drawable.shapes);
            Drawable targetShape = getResources().getDrawable(R.drawable.shape_drop_target);
    
            @Override
            public boolean onDrag(View v, DragEvent event) {
    
                // Handles each of the expected events
                switch (event.getAction()) {
    
                    //signal for the start of a drag and drop operation.
                    case DragEvent.ACTION_DRAG_STARTED:
                        // do nothing
                        break;
    
                    //the drag point has entered the bounding box of the View
                    case DragEvent.ACTION_DRAG_ENTERED:
                        v.setBackground(targetShape);   //change the shape of the view
                        break;
    
                    //the user has moved the drag shadow outside the bounding box of the View
                    case DragEvent.ACTION_DRAG_EXITED:
                        v.setBackground(normalShape);   //change the shape of the view back to normal
                        break;
    
                    //drag shadow has been released,the drag point is within the bounding box of the View
                    case DragEvent.ACTION_DROP:
                        // if the view is the bottomlinear, we accept the drag item
                        if(v == findViewById(R.id.bottomlinear)) {
                            View view = (View) event.getLocalState();
                            ViewGroup viewgroup = (ViewGroup) view.getParent();
                            viewgroup.removeView(view);
    
                            //change the text
                            TextView text = (TextView) v.findViewById(R.id.text);
                            text.setText("The item is dropped");
    
                            LinearLayout containView = (LinearLayout) v;
                            containView.addView(view);
                            btn.setVisibility(View.VISIBLE);
                            view.setVisibility(View.VISIBLE);
                        } else {
                            View view = (View) event.getLocalState();
                            view.setVisibility(View.VISIBLE);
                            Context context = getApplicationContext();
                            Toast.makeText(context, "You can't drop the image here",
                                    Toast.LENGTH_LONG).show();
                            break;
                        }
                        break;
    
                    //the drag and drop operation has concluded.
                    case DragEvent.ACTION_DRAG_ENDED:
                        v.setBackground(normalShape);   //go back to normal shape
    
                    default:
                        break;
                }
                return true;
            }
        }
    }
    
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />