如何设置背景图像ImageView

时间:2013-03-08 12:24:34

标签: android

ZoomImageViewer类型中的方法setBackgroudImage(byte [])不适用于下面的参数(ImageView)是我的ci遵循本教程Zoom image with effect ontouch android

          LinearLayout.LayoutParams paramsLO2 = new  
     LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     // button margins
     paramsLO2.setMargins(0, 20, 0, 0);
     // button height/width *pixels*

     btnLO.setOrientation(LinearLayout.VERTICAL);
     btnLO.setBackgroundColor(5); // not working correctly

     //buttons
     for (i =0;i <reqdata.length;i++)
         {
             LinearLayout li=new LinearLayout(this);
             li.setOrientation(LinearLayout.HORIZONTAL);
             final Button b1 = new Button(MainActivity1.this);
             final ImageView imageView = new ImageView(MainActivity1.this);
             ZoomImageViewer zoom = new ZoomImageViewer(this);

<!----  this line show problem       zoom.setBackgroudImage(imageView);----->

              setContentView(zoom);

             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);


             imageView.setOnTouchListener(this);

        //     int width = 160;
          //   int height = 50;

             LinearLayout.LayoutParams parms = new  
      LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
             parms.setMargins(0, 2, 0, 0);
             imageView.setLayoutParams(parms);

             li.addView(b1, paramsLO);        
             li.addView(imageView);
             btnLO.addView(li);

             b1.setText(reqdata[i].getSpinnerText());

             b1.setOnClickListener(new View.OnClickListener() 
                    {
                        public void onClick(View v) 
                        {               


                             imageView1 = imageView;

                             Intent pictureActionIntent = new  
         Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new  
   File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD,IMAGE_CAPTURE_NAME)));  

      startActivityForResult(pictureActionIntent,CAMERA_PICTURE);
                             b1.setClickable(false);


                          } 

                    });    

         }

     final Button b2 = new Button(MainActivity1.this);

     b2.setText("Submit");
     b2.setWidth(150);
     b2.setHeight(50);
     b2.setOnClickListener(new View.OnClickListener() 
     {
         public void onClick(View v) 
         {                           

             long visitID = dbConnector.saveVisit();

             for (i =0;i <reqdata.length;i++)
             {


   dbConnector.saveVisitDetail(listByte.get(i),visitID,Long.
 valueOf(reqdata[i].getValue()).longValue());
             }     
                Toast.makeText(getBaseContext(),    
   "Sucessful",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity1.this,  
    Main.class);        
                startActivity(intent);
                finish();
         }
     }); 
     btnLO.addView(b2, paramsLO2);
     btnLO.setGravity(Gravity.CENTER| Gravity.CENTER_HORIZONTAL);
     scroll.addView(btnLO);

     this.addContentView(scroll, new LayoutParams());

}



@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent   
    imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

            if (imageReturnedIntent.getExtras() != null) 
                {
                    // here is the image from camera
                    yourSelectedImage = (Bitmap)  
  imageReturnedIntent.getExtras().get("data");
                    ByteArrayOutputStream outStr = new  
  ByteArrayOutputStream();                

  yourSelectedImage.compress(CompressFormat.JPEG, 100, outStr);
                    blob = outStr.toByteArray();
                    yourSelectedImage =  
  BitmapFactory.decodeByteArray(blob, 0, blob.length);
                    imageView1.setImageBitmap(yourSelectedImage);
                    listByte.add(blob);

                }

    }

   class MyData1 {
        public MyData1( String spinnerText, String value ) {
            this.spinnerText = spinnerText;
            this.value = value;
        }

        public String getSpinnerText() {
            return spinnerText;
        }

        public String getValue() {
            return value;
        }

        public String toString() {
            return spinnerText;
        }

        String spinnerText;
        String value;
    }



   public boolean onTouch(View v, MotionEvent event) {

       ImageView view = (ImageView) v;

       view.setScaleType(ImageView.ScaleType.MATRIX);

       float scale;



       // Dump touch event to log

       dumpEvent(event);



       // Handle touch events here...

       switch (event.getAction() & MotionEvent.ACTION_MASK) {



       case MotionEvent.ACTION_DOWN: //first finger down only

          savedMatrix.set(matrix);

          start.set(event.getX(), event.getY());

          Log.d(TAG, "mode=DRAG" );

          mode = DRAG;

          break;

       case MotionEvent.ACTION_UP: //first finger lifted

       case MotionEvent.ACTION_POINTER_UP: //second finger lifted

          mode = NONE;

          Log.d(TAG, "mode=NONE" );

          break;

       case MotionEvent.ACTION_POINTER_DOWN: //second finger down

          oldDist = spacing(event);

          Log.d(TAG, "oldDist=" + oldDist);

          if (oldDist > 5f) {

             savedMatrix.set(matrix);

             midPoint(mid, event);

             mode = ZOOM;

             Log.d(TAG, "mode=ZOOM" );

          }

          break;



       case MotionEvent.ACTION_MOVE:

          if (mode == DRAG) { //movement of first finger

             matrix.set(savedMatrix);

             if (view.getLeft() >= -392){

                matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);

             }

          }

          else if (mode == ZOOM) { //pinch zooming

             float newDist = spacing(event);

             Log.d(TAG, "newDist=" + newDist);

             if (newDist > 5f) {

                matrix.set(savedMatrix);

                scale = newDist / oldDist; /*thinking i need to play around   
  with this value to limit it*/

                matrix.postScale(scale, scale, mid.x, mid.y);

             }

          }

          break;

       }



       // Perform the transformation

       view.setImageMatrix(matrix);



       return true; // indicate event was handled

    }



    private float spacing(MotionEvent event) {

       float x = event.getX(0) - event.getX(1);

       float y = event.getY(0) - event.getY(1);

       return FloatMath.sqrt(x * x + y * y);

    }



    private void midPoint(PointF point, MotionEvent event) {

       float x = event.getX(0) + event.getX(1);

       float y = event.getY(0) + event.getY(1);

       point.set(x / 2, y / 2);

    }



    /** Show an event in the LogCat view, for debugging */

    private void dumpEvent(MotionEvent event) {

       String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,

          "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };

       StringBuilder sb = new StringBuilder();

       int action = event.getAction();

       int actionCode = action & MotionEvent.ACTION_MASK;

       sb.append("event ACTION_" ).append(names[actionCode]);

       if (actionCode == MotionEvent.ACTION_POINTER_DOWN

             || actionCode == MotionEvent.ACTION_POINTER_UP) {

          sb.append("(pid " ).append(

          action >> MotionEvent.ACTION_POINTER_ID_SHIFT);

          sb.append(")" );

       }

       sb.append("[" );

       for (int i = 0; i < event.getPointerCount(); i++) {

          sb.append("#" ).append(i);

          sb.append("(pid " ).append(event.getPointerId(i));

          sb.append(")=" ).append((int) event.getX(i));

          sb.append("," ).append((int) event.getY(i));

          if (i + 1 < event.getPointerCount())

             sb.append(";" );

       }

       sb.append("]" );

       Log.d(TAG, sb.toString());

    }




   }

1 个答案:

答案 0 :(得分:0)

根据您指出的代码,似乎setBackgroundImage需要Bitmap作为输入参数。因此,您可以通过以下方式设置位图:

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
zoom.setBackgroudImage(bitmap);