2012-12-13 5 views
0

일반 오래된 Android 버튼의 Compound Drawable에 선을 그리려합니다. Button.getCompoundDrawables()[1]을 사용하여 버튼의 드로어 블 (drawable)을 얻으려는 것으로 시작했지만 선은 결코 나타나지 않습니다. 그래서, 나는 버튼에 복합 드로어 블을위한 XML 레이아웃에 실제 이미지를 넣었습니다. OK (사진의 주황색 사각형)가 작동하지만 전화가 회전하면 주황색 사각형의 크기가 조절되지 않고 버튼이 제대로 작동하지 않으므로 결국 너무 크게됩니다. 교대 또는 무언가를 할 때 getBounds()으로 전화해야합니까?Android 컴파운드 드로어로 그리기

드로어 블에 어떤 크기 조정이 적용되어야합니다. 알아 차릴 경우 빨간색 선은 가로 방향 모서리로 이동하지만 세로 방향 모퉁이로 이동하지 않아야합니다. 꺼져 있거나 뭔가 있어요. 주황색 사각형은 드로어 블 - [lhm] dpi/디렉토리에서 다른 크기로 존재하지만 가로 및 세로에 대해 두 개의 개별 레이아웃이 없습니다. 선을 그리기위한

코드 : 그것은 다른 사람을 도움이 경우

@Override 
      public View getView (int position, View convertView, ViewGroup parent) 
      {   
       View row = convertView; 
       if (row == null) 
       { 
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); 
        row = inflater.inflate (R.layout.monthview, parent, false); 

       } 


       btn_cell = (Button) row.findViewById (R.id.bcell); 
... 
        BitmapDrawable btn_draw = (BitmapDrawable) btn_cell.getCompoundDrawables()[1]; 

        if (btn_draw != null) 
        { 
         Log.d (TAG, "+++++++++++++ drawing line"); 
         Bitmap btn_bmp = btn_draw.getBitmap(); 
         Bitmap offscreen_bmp = Bitmap.createBitmap(btn_bmp.getWidth(), btn_bmp.getHeight(), btn_bmp.getConfig()); 
         BitmapDrawable offscreen_draw = new BitmapDrawable (offscreen_bmp); 
         offscreen_draw.setBounds (btn_draw.getBounds()); 

         Canvas c = new Canvas(offscreen_bmp); 

         // draw line 
         Paint p = new Paint(); 
         p.setAntiAlias(true); 
         p.setStrokeWidth(1); 
         p.setStyle(Style.FILL_AND_STROKE); 
         p.setColor(Color.RED); 

         c.drawBitmap (btn_bmp, 0, 0, p); 
         c.drawLine (0, 0, offscreen_bmp.getWidth(), offscreen_bmp.getHeight(), p); 

         (R.drawable.cal_left_arrow_off), null, null); 
         btn_cell.setCompoundDrawables(null, offscreen_draw, null, null); 
        } 

enter image description here enter image description here

답변

1

, 나는 이미지 뷰를 사용하여 멀리 버튼을 대신하고 끝났다. ImageView에 선을 그리는 방법과 관련하여 (분명히) 일반적인 문제가 발생했습니다. onDraw() 메서드에서 ImageView 하위 클래스와 드로잉 라인을 알고 있었지만 Eclipse의 XML 레이아웃 GUI에서 "사용자 정의"하위 클래스를 참조 할 수 있는지는 몰랐습니다. 필자가해야 할 일은 myImageView 클래스를 만든 다음 XML 소스 코드에서 참조하는 것뿐이었습니다. <ImageView>을 지정하는 대신 <com.example.myImageView>을 사용해야하고 모든 것이 작동했습니다.

 myImageView iv = (myImageView) row.findViewById (R.id.iv_draw); 
     iv.setBarLength (15); 
     iv.setOnClickListener (ocl); 

com.example.test.myImageView.java :

public class myImageView extends android.widget.ImageView 
{ 
    private final String TAG = this.getClass().getName(); 
    private int mBarLen = 5; 

    /** 
    * @param context 
    * @param attrs 
    */ 
    public myImageView (Context context, AttributeSet attrs) 
    { 
     super (context, attrs); 
    } 

    public void setBarLength (int length) 
    { 
     mBarLen = length; 
    } 

    @Override 
    protected void onDraw (Canvas canvas) 
    { 
     super.onDraw (canvas); 

     // draw 1px border 
     Paint p = new Paint(); 

     int x = 1; 
     int y = 1; 
     Rect bounds = canvas.getClipBounds(); 

     int x2 = bounds.right - 1; 
     int y2 = bounds.bottom - 1; 

     canvas.drawLine (x, y, x2, y, p); 
     canvas.drawLine (x2, y, x2, y2, p); 
     canvas.drawLine (x2, y2, x, y2, p); 
     canvas.drawLine (x, y2, x, y, p); 

     p.setColor (Color.RED); 
     p.setStrokeWidth (2); 
     int bx = x + 5; 
     int by = y + 5; 
     canvas.drawLine (bx, by, bx+mBarLen, by, p); 
     canvas.drawLine (bx, by, bx+mBarLen, by, p); 

    } 

} 

내가 정의 BaseAdapter이 때문에 getView() 방법에 myImageView에 배치 된 라인을 얻기에 myImageView을 사용했다

는 단지 문제 했다

XML 파일 : 덧붙여

<com.example.test.myImageView 
    android:id="@+id/iv_draw" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:adjustViewBounds="true" 
    android:minHeight="48dp" 
    android:minWidth="24dp" 
    android:scaleType="fitXY" /> 
+0

, 나는 같은 많은으로하여 ImageButton의 서브 클래스 결국 나는 원래 의도했던 것을 성취하기 위해 여기에 제시된 방법대로 – wufoo

관련 문제