2011-05-25 78 views
2

希望这是完成我的应用程序的最后一步。我需要在可点击的画布内创建一个位图,以便调用将播放视频的新活动(mp4)或在当前活动中播放视频。使用画布可点击的位图

显示画布和位图的类是我反复使用的类来显示缩略图的完整图像。图像ID通过一个意图传递。下面是完整的图像活动的代码(我这人非常的小白和使用本网站和其他人一起一步拼凑我的代码,在一个时间,所以我道歉,如果它不干净):

public class full_image extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(new BitmapView(this)); 
    getWindow().setBackgroundDrawableResource(R.drawable.bground); 
    getWindow().setWindowAnimations(0); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 

class BitmapView extends View { 
    public BitmapView(Context context) { 
     super(context); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     int imgid = getIntent().getIntExtra("Full",0); 
     Bitmap fullimage = BitmapFactory.decodeResource(getResources(),imgid); 
     Bitmap playButton = BitmapFactory.decodeResource(getResources(), R.drawable.bt_play); //Added for Video 
     Display display = getWindowManager().getDefaultDisplay(); 
     int fpWidth = fullimage.getWidth(); 
     int fpHeight = fullimage.getHeight(); 
     int playWidth = playButton.getWidth(); 
     int playHeight = playButton.getHeight(); 
     int screenWidth = display.getWidth(); 
     int screenHeight = display.getHeight(); 
     int leftPoint = screenWidth/2 - fpWidth/2; 
     int topPoint = screenHeight/2 - fpHeight/2; 
     int leftPlayPoint = screenWidth/2 - playWidth/2; 
     int topPlayPoint = screenHeight/2 - playHeight/2; 
     canvas.drawBitmap(fullimage,leftPoint,topPoint,null); 
     canvas.drawBitmap(playButton,leftPlayPoint,topPlayPoint,null); 

    } 
} 
} 

如果可能的话,我只想让playButton放置onClickListener,但如果它更容易,我可以使整个画布可点击(如果甚至可以)。

我读到另一个问题,有一个建议使用TouchEvent。我试着走那条路,但无法让它工作。这是正确的道路,我只是需要更多地使用它才能使其发挥作用?

感谢,J

其他的东西:这里

是一个代码片段我在另一个问题找到。我会在哪里将这些代码放入上面提供的代码中。

public boolean onTouchEvent(MotionEvent event){ 
int action = event.getAction(); 
int x = event.getX() // or getRawX(); 
int y = event.getY(); 

switch(action){ 
case MotionEvent.ACTION_DOWN: 
    if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth()) 
      && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) { 
     //tada, if this is true, you've started your click inside your bitmap 
    } 
    break; 
} 

}

回答

5

我想在画布上,我们不能对位图setOnClick。 但是我们可以使用onTouch方法。并检查触摸位图或不使用x-y触摸位置。

尝试onTouch方法对位图GET触摸这个代码...

if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth()) 
       && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) { 
      //tada, if this is true, you've started your click inside your bitmap 
     } 
+0

哪里会把onTouch方法?我试图把它放在OnDraw区域之外,但是我不能引用我的位图。当我尝试将它放入OnDraw区域时,出现错误。看到我上面的原始问题。我在底部添加了一些额外的代码。有关我在哪里添加此代码的任何建议?谢谢 – jbellomy 2011-05-26 06:52:29

+0

把它放在OnDraw和BitmapView(你的类)的内部,并创建位图的局部变量... – 2011-05-26 07:18:12

+0

你不知道我现在有多开心。第一次尝试。非常感谢你的帮助。 – jbellomy 2011-05-27 06:12:11