2017-12-14 9 views
0

모든 호출에서 주어진 위치에 포인터를 다시 그려야하는이 메소드를 호출합니다.동일한 메소드에서 addView 및 removeView를 사용하십시오.

ImageView ivPointer=null; 
public void moveCursor(Bitmap bmPuntero, int x, int y) 
{ 
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.gamelayout); 

    if (ivPointer!=null) 
     rl.removeView(ivPointer); 


    ivPointer = new ImageView(this); 

    ivPointer.setImageBitmap(bmPuntero); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(65, 65); 
    params.leftMargin = x; 
    params.topMargin = y; 
    rl.addView(ivPointer, params); 

} 

그 결과 비트 맵이 표시되지 않습니다. 뷰를 제거하는 선을 제거하면 비트 맵이 여러 번 그려지는 것을 볼 수 있으므로 추가 부분이 정확해야합니다.

+0

당신은 당신의 방법 이외의 레이아웃을 이동 시도? – Roljhon

+0

나는 지금 그것을 시도하고 동일한 결과 – takluiper

+0

그것은 나가 방법을 부르고 있던 주파수와 관련 있었다. 적은 빈도로 호출하면이 메서드를 호출하는 위치를 업데이트 할 때마다 이미지가 깜박입니다. – takluiper

답변

1

이 시도 :

{ 
    // Somewhere (in onCreate of the Activity for example): 

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.gamelayout); 
    ImageView ivPointer = initPointer(this, bmPuntero); // Get image from somewhere 
    rl.addView(ivPointer); 
} 


// To update the cursor's po 
public static void moveCursor(ImageView pointer, int x, int y) { 
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) pointer.getLayoutParams(); 
    params.leftMargin = x; 
    params.topMargin = y; 

    pointer.setLayoutParams(params); 
    pointer.requestLayout(); // Refresh the layout 
} 

// Call this method to initialise the pointer (in onCreate of your Activity 
// for example) 
public static ImageView initPointer(Context context, Bitmap bmp) { 
    // Define the LayoutParams 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(65, 65); 
    params.leftMargin = DEFAULT_POS_X; // TODO: Constants to be defined 
    params.topMargin = DEFAULT_POS_Y; 

    // Init the ImageView 
    ImageView pointer = new ImageView(context); 
    pointer.setImageBitmap(bmp); 
    pointer.setLayoutParams(params); 

    return pointer; 
} 
+0

위대한 작품! 감사 – takluiper

관련 문제