无法在图像视图上绘制矩形

问题描述:

我在通过图像视图绘制矩形时遇到问题。这是代码的和平。 Xml也在那里。我的整个担忧是,如果我们能够通过imageview绘制矩形。无法在图像视图上绘制矩形

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.cropimage); 
      paint=new Paint(); 
      bobj = new BaldBooth1(); 
      bm = BaldBooth1.bMap; 
      d = new BitmapDrawable(bm); 
      iv = ((ImageView) findViewById(R.id.image)); 
      iv.setImageDrawable(d); 
      createRectInView(iv); 

      ((ImageButton) findViewById(R.id.next)).setOnClickListener(this); 

     } 
     public void createRectInView(View v) { 
      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(3); 
      canvas=new Canvas(); 
      canvas.drawRect(50, 50, 80, 80, paint); 
      v.draw(canvas); 


     } 
+0

可能重复(http://stackoverflow.com/questions/9443300/cropping-image-using-rect) – 2012-02-25 13:10:24

+0

所以..?我有任何问题哈..?没有人在那里回答,所以已经发布这种方式.. F> O – arun 2012-02-25 13:19:49

+0

有点容易冒犯了,我们?是的,停止垃圾邮件问题。如果你没有得到任何答案,这可能是因为你没有给它足够的时间(1小时通常是不够的),或者你的问题缺乏一些质量*(它的确如此,几乎没有解释你正在尝试做什么;如果没有人能弄清楚你想要什么,他们不能帮你)*。 – 2012-02-25 13:23:04

你的方法createRectInView(View v)不超过ImageView的绘制一个矩形,它只是创造一个画布,绘制画布上的一个矩形,然后绘制的ImageView的是帆布上的内容,所以它不会做你期望的。

这里是一个可能的解决方案:你可以扩展的ImageView并覆盖其的onDraw()方法,例如..

public class ExtendedImageView extends ImageView { 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     paint.setStrokeWidth(3); 
     canvas.drawRect(50, 50, 80, 80, paint); 
    } 
} 

更新时间:

阿伦您好,我只是测试的代码,它工作正常。以下是详细信息:例如,您可以在com.abc.widget包中创建ExtendedImageView,因此在cropImage.xml文件中,将<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content">替换为<com.abc.widget.ExtendedImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content">。正如你所看到的,你只需要改变类名。再变onCreate()方法为:使用裁剪矩形图像]的

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.cropimage); 
      bobj = new BaldBooth1(); 
      bm = BaldBooth1.bMap; 
      d = new BitmapDrawable(bm); 
      iv = ((ImageView) findViewById(R.id.image)); 
      iv.setImageDrawable(d); 
      ((ImageButton) findViewById(R.id.next)).setOnClickListener(this); 

     } 
+0

嗯...是的,你是对的你的部分。但我试过你的解决方案它没有工作。可能是我在做错误的方式可以请你详细说明 – arun 2012-02-25 12:51:34

+0

嗨arun,更新了答案,希望它有帮助。 – 2012-02-25 13:05:58

+0

02-25 18:42:24.203:E/AndroidRuntime(4013):java.lang.RuntimeException:无法启动活动ComponentInfo {com.my.baldbooth1/com.my.baldbooth1.CropImage}:android.view.InflateException:二进制XML文件行#9:错误膨胀类com.my.baldbooth1.ExpandableImageView – arun 2012-02-25 13:15:33