【问题标题】:Android draw an image inside another imageAndroid 在另一个图像中绘制图像
【发布时间】:2014-04-15 08:57:49
【问题描述】:

如何使用 Canvas 在另一个图像中绘制图像; 像这样,看图:

把它放在像这样的地图应用 v2 中

 marker = gmap.addMarker(new MarkerOptions().title("test")
                        .position(new LatLng(0, 0))
                        .snippet("snipet test")
                        .icon(BitmapDescriptorFactory.fromBitmap(bitmap))

我已经在这样的矩形中画了一张图片

            InputStream inputStream = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);


            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawOval(rectF, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

如何做到这一点 请帮帮我

【问题讨论】:

    标签: android canvas google-maps-android-api-2 marker


    【解决方案1】:

    为你的画布设置一个你想要的框架形状的剪切路径:

    Path frame = new Path();
    frame.addCircle(centerX, centerY, radius, Direction.CW);
    canvas.getClipBounds(oldClipBounds); //see below
    canvas.clipPath(frame);
    

    之后你在画布中绘制的所有内容如果在圆圈之外都将不可见。

    如果您需要额外的绘图,这应该只在该框架之外进行,您可以稍后通过以下方式保护它:

    canvas.clipRect(oldClipBounds, Region.Op.REVERSE_DIFFERENCE);
    

    【讨论】:

      【解决方案2】:

      我找到了解决这个问题的方法: 您将图像放在画布的圆圈中,然后使用确切大小调整结果的大小,并将其放在标记图像中。

      public class IconMarker {
          public IconMarker(){}
      
          public Bitmap DrawMarker(String userid,int typeid,Resources rcs){
      
              //  image from database: ...InputStream inputStream = connection.getInputStream();
              //Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
              Bitmap img1=new UserInfoBd().getPhotoProfil(userid);
              if(img1==null) img1=BitmapFactory.decodeResource(rcs,R.drawable.espace_photo);
              Bitmap.Config conf = Bitmap.Config.ARGB_8888;
              Bitmap bmp = BitmapFactory.decodeResource(rcs,
                      typeid);
              Bitmap output = Bitmap.createBitmap(bmp.getWidth(),
                      bmp.getHeight(), Bitmap.Config.ARGB_8888);
      
              Canvas canvas1 = new Canvas(output);
              canvas1.drawBitmap(bmp, 0,0, null);
              Bitmap output1 = Bitmap.createBitmap(img1.getWidth(),
                      img1.getHeight(), Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(output1);
      
              final int color = 0xff424242;
              final Paint paint = new Paint();
              final Rect rect = new Rect(0, 0, img1.getWidth(), img1.getHeight());
              final RectF rectF = new RectF(rect);
      
      
              paint.setAntiAlias(true);
              canvas.drawARGB(0, 0, 0, 0);
              paint.setColor(color);
              canvas.drawOval(rectF, paint);
      
              paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
              canvas.drawBitmap(img1, rect, rect, paint);
              Bitmap img=getResizedBitmap(output1,bmp.getHeight()*3/4-4,bmp.getWidth()*3/4);
      
              canvas1.drawBitmap(img,(float)4.7,(float)3.5,null);
      
              return output;
      
          }
          public  Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
      
              int width = bm.getWidth();
      
              int height = bm.getHeight();
      
              float scaleWidth = ((float) newWidth) / width;
      
              float scaleHeight = ((float) newHeight) / height;
      
              // CREATE A MATRIX FOR THE MANIPULATION
              Matrix matrix = new Matrix();
      
              // RESIZE THE BIT MAP
              matrix.postScale(scaleWidth, scaleHeight);
      
              // RECREATE THE NEW BITMAP
              Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
      
              return resizedBitmap;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-19
        • 2012-06-15
        • 2012-04-20
        • 2012-10-30
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        相关资源
        最近更新 更多