如何将图像放在画布的八个坐标上

时间:2014-03-27 09:29:32

标签: android

![如何将图片放在android中的8个坐标中请帮我解决这个问题 问题,我需要通过下一个新的8个坐标移动这个图像,这些值来自文本文件帮我找到一个很好的解决方案这个问题][1]

如何在android中的画布中将图像放在8个坐标中请帮我解决这个问题 问题,我需要通过下一个新的8个坐标移动这个图像,这些值来自文本文件帮我解决这个问题

cordi.txt(有这样的)

200 110 600 110 200 740 600 740

200 111 600 111 200 741 600 741

200 112 600 112 200 742 600 742

200 113 600 113 200 743 600 743

200 114 600 114 200 744 600 744

//在这里从文本文件中获取坐标

//在这里我正在绘制坐标线,但我需要在坐标

中放置图像

但是我发现通过这种方法将位图图像放在X,Y的特定位置  它工作在线性运动上下移动确定完美的工作,但当没有线性坐标意味着(图像旋转发生)此时我无法找到位图的旋转角度与轴可以给你指导线

    class image_placer extend view { 
            BufferedReader reader = new BufferedReader(new  InputStreamReader(context1.getAssets().open("cordi.txt")));
                      String line;
                      while ((line = reader.readLine()) != null)
                  {
                       String[]  saLineElements = line.split("  ");
                      X1.add(Integer.parseInt(saLineElements[0].toString().trim()));
                      Y1.add(Integer.parseInt(saLineElements[1].toString().trim()));

                      X2.add(Integer.parseInt(saLineElements[2].toString().trim()));
                      Y2.add(Integer.parseInt(saLineElements[3].toString().trim()));

                      X3.add(Integer.parseInt(saLineElements[4].toString().trim()));
                      Y3.add(Integer.parseInt(saLineElements[5].toString().trim()));

                      X4.add(Integer.parseInt(saLineElements[6].toString().trim()));
                      Y4.add(Integer.parseInt(saLineElements[7].toString().trim()));  
                  }
                  reader.close();
               } 
              catch (FileNotFoundException e){
                  System.err.println(
                     "FileNotFoundException: " + e.getMessage());
              } catch (IOException e) {
                  e.printStackTrace();
              }catch (NumberFormatException e) {
                 System.out.println(""+e.getMessage());
            }
// now in on draw method i need to place the image in that coordinates  i=0 (Globally declared )
            public void onDraw(Canvas canvas) {  
                  if(i>=X1.size())
            {
                i=0;
                X=X1.get(i);
                Y=Y1.get(i);
                 dx=X2.get(i)-X1.get(i);
                dy=Y3.get(i)-Y1.get(i);
                Path drawPath=new Path();
                drawPath.moveTo(X1.get(i), Y1.get(i));
                drawPath.lineTo(X2.get(i), Y2.get(i));
                drawPath.lineTo(X4.get(i), Y4.get(i));
                drawPath.lineTo(X3.get(i), Y3.get(i));
                drawPath.lineTo(X1.get(i), Y1.get(i));    
               canvas.drawPath(drawPath, mPaint);

          }

            else
            {
                X=X1.get(i);
                Y=Y1.get(i);
                dx=X2.get(i)-X1.get(i);
                dy=Y3.get(i)-Y1.get(i);
                Path drawPath=new Path();
                drawPath.moveTo(X1.get(i), Y1.get(i));
                drawPath.lineTo(X2.get(i), Y2.get(i));
                drawPath.lineTo(X4.get(i), Y4.get(i));
                drawPath.lineTo(X3.get(i), Y3.get(i));
                drawPath.lineTo(X1.get(i), Y1.get(i));    
               canvas.drawPath(drawPath, mPaint);
                i++;
            }
     float scaleWidth = (dx) / ballW;
        float scaleHeight = (dy) / ballH;
        if(i<=X1.size())
        {
        Matrix matrix_rev = new Matrix();
        matrix_rev.postScale(scaleWidth, scaleHeight);
        resizedBitmap = Bitmap.createBitmap(reverse, 0, 0,
                ballW, ballH, matrix_rev, true);
        }else{
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            if(i<=X1.size()-1)
           resizedBitmap = Bitmap.createBitmap(ball, 0, 0,
                    ballW, ballH, matrix, true);
        }

         canvas.drawBitmap(bitmap_img, X,Y, null);
             invalidate();
    }}

1 个答案:

答案 0 :(得分:0)

您可以使用

RectF dstRect=new RectF(x1,y1,width,height);
Canvas can=new Canvas();
can.drawBitmap(bitmap, null, dstRect, paint);