1

我的应用特点:

  • 从相机或图片库拍照;//完毕
  • 收集有趣的贴纸;//完毕
  • 通过电子邮件、彩信等分享创作的滑稽照片。//完成
  • 将“贴纸”照片保存在图片库中;//完毕
  • 移动、缩放和旋转贴纸图像;
  • 在这个问题中..我面临困难...我可以添加,但粘贴两个或更多后无法选择贴纸...当我在图像中添加两个或三个贴纸时,我不能单独选择贴纸。或者通过选择贴纸自动改变。

从上周开始面对这个问题......

谁能建议我任何示例程序或支持的 lib 文件?

我试过Aviary,但它不适合这个应用程序。

以下是我的代码列表:

     public class MyMain extends Activity implements OnClickListener {
            private static final int FROM_GALLERY = 200, CAMERA_PIC_REQUEST = 201,
                    SELECT_STICK_IMAGE = 10, FROM_SAVE_LIB = 11;
            private ImageView mainImage, imgNew, imgSave, cat, stickyImage, done, flip,back,trash;
            private RelativeLayout catWangMainImgLayout, rl_mainImageBottamBar1,
            rl_mainImageBottamBar2;
            public static int width, height, imWidth;
            public OnTouchListener onTouchListenerStickImage = null;
            private Bitmap camera_thumbnail;
            static int l=0,m=0;
            Pinch pig;
            String image_id;
            ArrayList stickyItemId = new ArrayList();
            static Bitmap join;
            private LinearLayout carHead, editImage;
            private OnClickListener stickyImageListner, doneListener;
            ArrayList sticky_list = new ArrayList();
            public static int drag_x, drag_y;  
            int index = -1;
            boolean stk_in_rect = false;
            ImageView imageView_stky, imageView3;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                setContentView(R.layout.cat_wang_main);      

                Info.sticky_param = new ArrayList();

                String type = getIntent().getExtras().getString("action_type");

                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);
                Info.widthAfterZoom = Info.screenWidth = width = metrics.widthPixels;
                imWidth = width - width / 15;
                Info.screenHight = height = metrics.heightPixels;

                                stickyImage.setImageDrawable(getResources().getDrawable(Integer.parseInt(stickyItemId.get(index).toString())));

                                stickyImage.setBackgroundResource(R.drawable.image_border);
                                stickyImage.setPadding(20, 20, 20, 20);
                                Bitmap bmap3 = loadBitmapFromView(stickyImage);

                                imageView3.setOnTouchListener(null);
                                Pinch.matrix = Info.getMat(index);

                                imageView3.setOnTouchListener(Info.onTch);
                                imageView3.setImageBitmap(bmap3);

                                imageView3.bringToFront();

                                setMainBottanEditBar();
                            }      
                        }
                        return true;
                    }
                };  

                     doneListener = new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            l=0;
                            m=0;
                            imageView_stky = (ImageView) catWangMainImgLayout.getChildAt(Info.current_matrix_id+1);

                            imageView_stky.setImageBitmap(null);
                            imageView_stky.setAlpha(255);

                            stickyImage = new ImageView(CatWangMain.this);              
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Info.screenWidth, Info.screenWidth);
                            params.addRule(RelativeLayout.CENTER_IN_PARENT);
                            stickyImage.setLayoutParams(params);

                            stickyImage.setPadding(20, 20, 20, 20);

                            sticky_list.add(stickyImage);

                            Bitmap bmap2 = loadBitmapFromView(stickyImage);
                            imageView_stky.setOnTouchListener(null);
                            imageView_stky.setOnTouchListener(onTouchListenerStickImage);

                            imageView_stky.setImageBitmap(bmap2);


                            setSelectStickyMode();
                            Info.addMat(Pinch.matrix,index);              

                            Pinch.editMood = false;
                            //int i =0;
                            Info.sticky_param.set(Info.current_matrix_id,new SavedStickyImageParam(Info.ceterOfX, Info.ceterOfY, Info.widthAfterZoom, Info.rotation));

                        } catch (NumberFormatException e) {

                            e.printStackTrace();
                        } catch (NotFoundException e) {

                            e.printStackTrace();

                        }

                    }
                };

            }

            void camera() {
                Context context = this;
                PackageManager packageManager = context.getPackageManager();
                if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
                    // yes
                    Log.i("camera", "This device has camera!");
                    Intent cameraIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
                } else {
                    // no
                    Log.i("camera", "This device has no camera!");
                    pickImage();
                }
            }

            void pickImage() {

                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");

                startActivityForResult(photoPickerIntent, FROM_GALLERY);
            }

            void createStickyImage(Bitmap bitmap, int id) {

                pig = new Pinch(this, catWangMainImgLayout);
                pig.addImage(bitmap, id);

            }
                super.onActivityResult(requestCode, resultCode, data);

                int req = requestCode;
                switch (req) {
                case FROM_GALLERY:
                    setMainImageBg(resultCode, data);

                    break;
                case SELECT_STICK_IMAGE:
                    addStickyImage(resultCode, data); 

                    break;
                case CAMERA_PIC_REQUEST:
                    if (requestCode == CAMERA_PIC_REQUEST) {
                        if (resultCode == RESULT_OK) {
                            // data.getExtras()
                            camera_thumbnail = (Bitmap) data.getExtras().get("data");
                            mainImage = (ImageView) findViewById(R.id.img_mainImg);
                            mainImage.setImageBitmap(camera_thumbnail);

                        } else {
                            finish();
                        }
                    } else {
                    }
                    break;
                case FROM_SAVE_LIB:
                    Bundle b = getIntent().getExtras();
                    String sd = (String) b.getString("fromSaveImag");
                    if (resultCode == 13) {
                        callDialog_SaveToLib();
                    }
                    break;
                default:
                    break;
                }
            }
            void addStickyImage(int resultCode, Intent data){
                if (resultCode == RESULT_OK) {
                    //sticky_param_added = false;
                    index = index +1;
                    Pinch.drag_x_value = Info.screenWidth/2;
                    Info.ceterOfX = Info.screenWidth/2;
                    Info.ceterOfY = Info.screenWidth/2;
                    Info.widthAfterZoom = Info.screenWidth;
                    Info.rotation = 0;

                    image_id = data.getStringExtra("img");
                    stickyItemId.add(image_id);
                    Toast.makeText(getApplicationContext(),"" + Integer.parseInt(image_id), Toast.LENGTH_SHORT).show();

                    stickyImage = new ImageView(this);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Info.screenWidth, Info.screenWidth);
                    params.addRule(RelativeLayout.CENTER_IN_PARENT);

                    stickyImage.setLayoutParams(params);

                    stickyImage.setImageDrawable(getResources().getDrawable(
                            Integer.parseInt(stickyItemId.get(stickyItemId.size() - 1).toString())));
                    stickyImage.setBackgroundResource(R.drawable.image_border);
                    stickyImage.setPadding(20, 20, 20, 20);

                    Bitmap bmap = loadBitmapFromView(stickyImage);

                    createStickyImage(bmap, 1);
                    setMainBottanEditBar();
                }
            }
            public static Bitmap loadBitmapFromView(View v) {
                Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width,v.getLayoutParams().height, Bitmap.Config.ARGB_8888);

                Canvas c = new Canvas(b);
                v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
                v.draw(c);
                return b;
            }
                      void callDialogToGoHome() {

                AlertDialog alert;
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(CatWangMain.this);          
                alertBuilder.setMessage("Are you sure you want to start over?");

                alertBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });          

                alertBuilder.setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        finish();
                    }
                });

                alert = alertBuilder.create();
                alert.show();
            }

            void callDialog_SaveToLib() {
                AlertDialog alertSaveLIb;
                AlertDialog.Builder alertBuilderSaveLib = new AlertDialog.Builder(
                        CatWangMain.this);
                alertBuilderSaveLib.setTitle("OMG !");
                alertBuilderSaveLib.setMessage("yo! we totally saved that graphic !");
                alertBuilderSaveLib.setPositiveButton("Thanks",
                        new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });
                alertSaveLIb = alertBuilderSaveLib.create();
                alertSaveLIb.show();
            }

            @Override
            public void onClick(View v) {
                int id = v.getId();
                switch (id) {      
                case R.id.img_new:          
                    callDialogToGoHome();
                    break;
                case R.id.img_save:
                    try {
                        mainImage.buildDrawingCache();
                        Bitmap viewBitmap1 = mainImage.getDrawingCache();

                        if (catWangMainImgLayout.getChildCount() > 1) {


                            join = madeJoinBitmap(viewBitmap1);
                        } else {
                            join = viewBitmap1;
                        }
                    } catch (Exception e) {

                        e.printStackTrace();

                    }
           Intent intentFlip = new Intent(CatWangMain.this, SaveImage.class);
                    ByteArrayOutputStream blob = new ByteArrayOutputStream();
                    join.compress(CompressFormat.PNG, 80 , blob);

                    byte[] bitmapdata = blob.toByteArray();
                    intentFlip.putExtra("bitmap", bitmapdata);

                    startActivityForResult(intentFlip, FROM_SAVE_LIB);

                    break;
                case R.id.img_cat:
                    Intent catWongItems = new Intent(this, CatWongStickyItems.class);
                    startActivityForResult(catWongItems, 10);

                    break;
                default:
                    break;
                }
            }

            private Bitmap madeJoinBitmap(Bitmap main) {
                ImageView imageView = null;
                Bitmap _bmp2 = null;
                Bitmap bmOverlay = Bitmap.createBitmap(main.getWidth(), main.getHeight(), main.getConfig());
                Canvas canvas = new Canvas(bmOverlay);
                canvas.drawBitmap(main, new Matrix(), null);
                if (catWangMainImgLayout.getChildCount() > 1) {
                    for (int y = 0; y < sticky_list.size(); y++) {

                        imageView = (ImageView) sticky_list.get(y);
                        imageView.buildDrawingCache();
                        _bmp2 = imageView.getDrawingCache();

                        if((Matrix)Info.matrixList.get(y) != null)
                        {
                            canvas.drawBitmap(_bmp2, (Matrix)Info.matrixList.get(y),null);
                        }else {
                            canvas.drawBitmap(_bmp2, new Matrix(), null);
                        }                
                    }
                }

                return bmOverlay;
            }

            void setMainBottanEditBar() {
                editImage = (LinearLayout) findViewById(R.id.ll_edit);
                editImage.setVisibility(View.VISIBLE);

                rl_mainImageBottamBar1 = (RelativeLayout) findViewById(R.id.rl_bottam_bar);
                rl_mainImageBottamBar1.setVisibility(View.INVISIBLE);

                rl_mainImageBottamBar2 = (RelativeLayout) findViewById(R.id.rl_bottam_bar2);
                rl_mainImageBottamBar2.setVisibility(View.VISIBLE);

                carHead = (LinearLayout) findViewById(R.id.ll_catHead);
                carHead.setVisibility(View.INVISIBLE);

                done = (ImageView) findViewById(R.id.img_done);
                done.setOnClickListener(doneListener);

                back = (ImageView) findViewById(R.id.img_back);
                back.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        try {
                            imageView_stky = (ImageView) catWangMainImgLayout.getChildAt(Info.current_matrix_id+1);
                            if(l==0)
                            {
                                imageView_stky.setAlpha(50);

                                l=1;
                            }
                            else
                            {
                                l=0;
                                imageView_stky.setAlpha(255);
                            }

                            setMainBottanEditBar();
                        } catch (Exception e) {

                            e.printStackTrace();

                        }   
                    }

                });          

                flip = (ImageView) findViewById(R.id.img_flip);
                flip.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {          
                        Toast.makeText(getApplicationContext(), "flip.. in progress",Toast.LENGTH_SHORT).show();

                    }

                });
                trash = (ImageView) findViewById(R.id.img_trash);
                trash.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {

                            imageView_stky = (ImageView) catWangMainImgLayout.getChildAt(Info.current_matrix_id+1);
                            imageView_stky.setImageBitmap(null);
                            imageView_stky.setAlpha(255);

                            stickyImage = new ImageView(CatWangMain.this);              
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                    Info.screenWidth, Info.screenWidth);
                            params.addRule(RelativeLayout.CENTER_IN_PARENT);
                            stickyImage.setLayoutParams(params);

                            stickyImage.setImageDrawable(getResources().getDrawable(Integer.parseInt((String)stickyItemId.get(index))));
                            stickyImage.setPadding(20, 20, 20, 20);

                            sticky_list.add(stickyImage);

                            Bitmap bmap2 = loadBitmapFromView(stickyImage);
                            imageView_stky.setOnTouchListener(null);
                            imageView_stky.setOnTouchListener(onTouchListenerStickImage);
                            imageView_stky.setImageBitmap(bmap2);
                            setSelectStickyMode();
                            Info.addMat(Pinch.matrix, Info.current_matrix_id);              
                            Pinch.editMood = false;

                            Info.sticky_param.set(Info.current_matrix_id,new SavedStickyImageParam(Info.ceterOfX, Info.ceterOfY, Info.widthAfterZoom, Info.rotation));

                            catWangMainImgLayout.removeViewAt(catWangMainImgLayout.getChildCount() - 1);
                            catWangMainImgLayout.invalidate();
                            stickyItemId.remove(index);
                            setSelectStickyMode();

                            sticky_list.remove(index);              
                            Info.sticky_param.remove(index);
                            Info.deleteMat(index);          
                           Info.current_matrix_id = Info.current_matrix_id - 1;  

                            index=index-1;;
                        } catch (Exception e) {

                            e.printStackTrace();
                        }
                    }
                });
            }

            void setSelectStickyMode() {
                editImage = (LinearLayout) findViewById(R.id.ll_edit);
                editImage.setVisibility(View.INVISIBLE);

                rl_mainImageBottamBar1 = (RelativeLayout) findViewById(R.id.rl_bottam_bar);
                rl_mainImageBottamBar1.setVisibility(View.VISIBLE);

                rl_mainImageBottamBar2 = (RelativeLayout) findViewById(R.id.rl_bottam_bar2);
                rl_mainImageBottamBar2.setVisibility(View.INVISIBLE);
                          carHead = (LinearLayout) findViewById(R.id.ll_catHead);
                carHead.setVisibility(View.VISIBLE);
            }

            private Bitmap decodeFile(File f) {
                try {

                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
                    BitmapFactory.decodeStream(new FileInputStream(f), null, o);

                    final int REQUIRED_SIZE = 70;
                    int scale = 1;
                    while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                            && o.outHeight / scale / 2 >= REQUIRED_SIZE) scale *= 2;
                 BitmapFactory.Options o2 = new BitmapFactory.Options();
                    o2.inSampleSize = scale;
                    return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
                } catch (FileNotFoundException e) {
                }
                return null;
            }
        }

        ///////////////////////////////////////

        public class Info {
            static int screenWidth = CatWangMain.width, screenHight, stickyImageID = 90;
            static OnTouchListener onTch;       

            public static ArrayList matrixList;
            public static Point p1;
            public static Point p2;
            public static int ceterOfX, ceterOfY, widthAfterZoom = screenWidth, rotation;
            public static ArrayList sticky_param;//  = new ArrayList();;
            public static int current_matrix_id = -1;


            public static boolean checkPointInRect(float rx, float ry, int rw, int rh, int rot, float px, float py, boolean x){

                double rotRad = (Math.PI * rot) / 180;
                double dx = px - rx;
                double dy = py - ry;

                double h1 = Math.sqrt(dx * dx + dy * dy);
                double currA = Math.atan2(dy,dx);

                double newA = currA - rotRad;
                double x2 = Math.cos(newA) * h1;
                double y2 = Math.sin(newA) * h1;

                if (x2 > - 0.5 * rw && x2 < 0.5 * rw && y2 > - 0.5 * rh && y2 < 0.5 * rh)
                    return true;
                    return false;      
            }

            public static void addMat(Matrix m){

                if(matrixList == null){
                    matrixList = new ArrayList();
                }
            }
            public static void addMat(Matrix m, int position){

                    matrixList.set(position, m);

            }
            public static void deleteMat(int pos){        
                matrixList.remove(pos);
            }

            public static Matrix getMat(int pos){

                return (Matrix)matrixList.get(pos);
            }
            public static int getMatrixSize(){
                       return matrixList.size();

            }
        }

        ////////////////////////////////////

        public class Pinch //implements OnTouchListener
        {          
            public static Matrix matrix = new Matrix();
            public static Matrix savedMatrix = new Matrix();
            RelativeLayout parent;
            Activity context;         

            static final int NONE = 0;
            static final int DRAG = 1;
            static final int ZOOM = 2;
            int mode = NONE, preRotate, rotate;
            public  int x,y;        

            PointF start = new PointF();
            PointF mid = new PointF();
            float oldDist = 1f;
            private boolean singleTouch;
            public static float scale = 1;
            public static int drag_x_value = Info.screenWidth/2, drag_y_value = Info.screenWidth/2;
            public static boolean editMood;              
            OnTouchListener touchLis = new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    ImageView view = (ImageView) v;        
                    dumpEvent(event);
                        }else {
                            Info.ceterOfX = (int)mid.x;
                            Info.ceterOfY = (int)mid.y;
                        }              

                    case MotionEvent.ACTION_POINTER_UP:

                    return true; // indicate event was handled
                }
            };

            public Pinch(Activity context, RelativeLayout parent) {
                this.context = context;
                this.parent = parent;                  
            }

            public ImageView addImage(Bitmap image, int id) {                   
                matrix = new Matrix();
                ImageView tempImage = new ImageView(context);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);      
                params.addRule(RelativeLayout.CENTER_IN_PARENT);
                tempImage.setId(Info.stickyImageID);
                Info.stickyImageID = Info.stickyImageID + 1;        
                tempImage.setImageBitmap(image);        
                tempImage.setLayoutParams(params);               
                Info.onTch = touchLis;
                tempImage.setOnTouchListener(Info.onTch);
                matrix.postScale(01.00f, 01.00f);
                tempImage.setScaleType(ScaleType.MATRIX);
                tempImage.setImageMatrix(matrix);                  
                Info.ceterOfY = Info.ceterOfX;        
                Info.addMat(Pinch.matrix);
                Info.current_matrix_id = Info.getMatrixSize()-1;
                Info.sticky_param.add(new SavedStickyImageParam(Info.ceterOfX, Info.ceterOfY, Info.widthAfterZoom, Info.rotation));
                parent.addView(tempImage);
                return tempImage;
            }
4

1 回答 1

0

通过 setTag() 为每个图像视图设置标签然后,在 On Click Event 中,您可以通过 getTag() 选择图像。

于 2012-10-16T09:10:38.293 回答