Android 프로 그래 밍 은 비 뚤 어 진 그림 그리 기 기능 예 시 를 실현 합 니 다.

이 사례 는 안 드 로 이 드 프로 그래 밍 이 그림 을 왜곡 하 는 그리 기 기능 을 실현 하 는 것 을 보 여 준다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
애니메이션 효 과 를 실현 하기 위해 drawBitmapMess 방법 으로 그림 을 왜곡 하고 타 이 머 를 사용 하여 100 밀리초 의 주파수 로 원형 궤적 에 따라 그림 을 왜곡 합 니 다.
왜곡 의 관건 은 verts 배열 을 만 드 는 것 이다.이 예 는 처음에 verts 배열 의 초기 값 이 될 것 입 니 다.일정한 수평 과 수직 간격 이 있 는 그물 점 좌표 입 니 다.그리고 warp 방법 을 통 해 일정한 수학 방법 에 따라 verts 배열 의 좌 표를 변화 시킨다.핵심 부분의 코드 는 다음 과 같다.
기본 변 수 를 정의 합 니 다:MyView 는 비 뚤 어 진 그림 을 표시 하 는 사용자 정의 view 입 니 다.angle 는 원형 궤적 의 현재 각도 입 니 다.

private static Bitmap bitmap;
private MyView myView;
private int angle = 0;         //          
private Handler handler = new Handler()
{
   public void handleMessage(Message msg)
   {
     switch (msg.what)
     {
       case 1:
         Random random = new Random();
         //          
         int centerX = bitmap.getWidth() / 2;
         int centerY = bitmap.getHeight() / 2;
         double radian = Math.toRadians((double) angle);
         //       、                   
         int currentX = (int) (centerX + 100 * Math.cos(radian));
         //       、                   
         int currentY = (int) (centerY + 100 * Math.sin(radian));
         //   View,            
         myView.mess(currentX, currentY);
         angle += 2;
         if (angle > 360)
           angle = 0;
         break;
     }
     super.handleMessage(msg);
   }
};
private TimerTask timerTask = new TimerTask()
{
   public void run()
   {
     Message message = new Message();
     message.what = 1;
     handler.sendMessage(message);
   }

다음은 사용자 정의 view,MyView 의 구체 적 인 내용 입 니 다.

private static class MyView extends View
{
    private static final int WIDTH = 20;
    private static final int HEIGHT = 20;
    private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);
    private final float[] verts = new float[COUNT * 2];
    private final float[] orig = new float[COUNT * 2];
    private final Matrix matrix = new Matrix();
    private final Matrix m = new Matrix();
    //   verts    
    private static void setXY(float[] array, int index, float x, float y)
    {
      array[index * 2 + 0] = x;
      array[index * 2 + 1] = y;
    }
    public MyView(Context context)
    {
      super(context);
      setFocusable(true);
      bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
      float w = bitmap.getWidth();
      float h = bitmap.getHeight();
      int index = 0;
      //   verts orig      ,           ,             verts
      //   ,   verts           orig   
      for (int y = 0; y <= HEIGHT; y++)
      {
        float fy = h * y / HEIGHT;
        for (int x = 0; x <= WIDTH; x++)
        {
          float fx = w * x / WIDTH;
          setXY(verts, index, fx, fy);
          setXY(orig, index, fx, fy);
          index += 1;
        }
      }
      matrix.setTranslate(10, 10);
      setBackgroundColor(Color.WHITE);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
      canvas.concat(matrix);
      canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts, 0, null, 0,null);
    }
    //          ,             (        ),   cx cy  ,
    //      verts       
    private void warp(float cx, float cy)
    {
      final float K = 100000;  //     ,      (       )
      float[] src = orig;
      float[] dst = verts;
      //           verts       
      for (int i = 0; i < COUNT * 2; i += 2)
      {
        float x = src[i + 0];
        float y = src[i + 1];
        float dx = cx - x;
        float dy = cy - y;
        float dd = dx * dx + dy * dy;
        float d = FloatMath.sqrt(dd);
        float pull = K / ((float) (dd *d));
        if (pull >= 1)
        {
          dst[i + 0] = cx;
          dst[i + 1] = cy;
        }
        else
        {
          dst[i + 0] = x + dx * pull;
          dst[i + 1] = y + dy * pull;
        }
      }
    }
    //   MyView           。    handleMessage      
    public void mess(int x, int y)
    {
      float[] pt ={ x, y };
      m.mapPoints(pt);
      //     verts    
      warp(pt[0], pt[1]);
      invalidate();
    }
  }
}

다음은 Activity 의 onCreate 방법 입 니 다.

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    myView = new MyView(this);
    setContentView(myView);
    Timer timer = new Timer();
    //      
    timer.schedule(timerTask, 0, 100);
}

다음은 비 뚤 어 진 후의 효 과 를 살 펴 보 자.서로 다른 시각 에 그림 은 서로 다른 비 뚤 어 진 효 과 를 나타 낸다.

더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.,,,,,,,
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기