Android layout 如何直接绘制到LinearLayout';s画布-Android

Android layout 如何直接绘制到LinearLayout';s画布-Android,android-layout,android-canvas,Android Layout,Android Canvas,我有一个线性布局,上面有3张图片。每当用户单击ImageView时,我都希望在ImageView上绘制一个周围的矩形。我知道如何在图像视图上绘制一些东西,但我想直接在线性布局上绘制 我该怎么做? 我已经编写了以下代码: setContentView(R.id.anaekran2); LinearLayout linLayout=(LinearLayout)findViewById(R.id.layout01) 是否有类似于linLayout.getCanvas()? 我找不到类似的函数,但我找到

我有一个线性布局,上面有3张图片。每当用户单击ImageView时,我都希望在ImageView上绘制一个周围的矩形。我知道如何在图像视图上绘制一些东西,但我想直接在线性布局上绘制

我该怎么做? 我已经编写了以下代码:

setContentView(R.id.anaekran2);
LinearLayout linLayout=(LinearLayout)findViewById(R.id.layout01)

是否有类似于
linLayout.getCanvas()
? 我找不到类似的函数,但我找到了
linLayout.getDrawingCache()
。所以

Bitmap b=linLayout.getDrawingCache();

画布c=新画布(b);//此行给出了一个错误,为什么?

您应该创建一个自定义线性布局。覆盖线性布局的onDraw方法可以解决您的问题。这是自定义线性布局代码

public class CustomLinearLayout extends LinearLayout {

private Paint mPaint;
private int mClickedChild = -1;

public CustomLinearLayout(Context context) {
    super(context);
    setWillNotDraw(false);
    createPaint();
}

public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWillNotDraw(false);
    createPaint();
}

public CustomLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setWillNotDraw(false);
    createPaint();
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    initializeChildrenClickEvent();
}

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

    if(mClickedChild != -1){
        View child = getChildAt(mClickedChild);
        canvas.drawRect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom(), mPaint);
    }
}

private void initializeChildrenClickEvent(){
    final int childCount = getChildCount();
    OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            for(int i = 0; i < childCount; i++){
                if(getChildAt(i).equals(view)){
                    mClickedChild = i;
                    break;
                }
            }
            invalidate();
        }
    };

    for(int i = 0; i < childCount; i++){
        getChildAt(i).setOnClickListener(clickListener);
    }
}

private void createPaint(){
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(Color.RED);
    mPaint.setStrokeWidth(5f);
}
}
公共类CustomLinearLayout扩展了LinearLayout{
私人油漆;
private int mClickedChild=-1;
公共CustomLinearLayout(上下文){
超级(上下文);
setWillNotDraw(假);
createPaint();
}
公共CustomLinearLayout(上下文、属性集属性){
超级(上下文,attrs);
setWillNotDraw(假);
createPaint();
}
公共CustomLinearLayout(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
setWillNotDraw(假);
createPaint();
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
超级在线布局(更改,l,t,r,b);
初始化儿童ClickEvent();
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
如果(mClickedChild!=-1){
View child=getChildAt(mClickedChild);
drawRect(child.getLeft()、child.getTop()、child.getRight()、child.getBottom()、mpain);
}
}
private void initializeChildrenClickEvent(){
final int childCount=getChildCount();
OnClickListener clickListener=新建OnClickListener(){
@凌驾
公共void onClick(视图){
for(int i=0;i
下面是xml中自定义布局的用法

<CustomLinearLayout      
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/image_view_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/image_view_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/image_view_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:src="@drawable/ic_launcher" />

</CustomLinearLayout>


错误是什么?绘图缓存启用了吗?嗨,谢谢你的回复。我忘记检查“b”值了。它是空的!但即使调用linLayout.setDrawingCacheEnabled(true),它也是空的;在调用linLayout.getDrawingCache()之前;是否可以直接绘制到LinearLayout?如果我动态地将子对象添加到LinearLayout,并且当选择该子对象时,在该子对象上绘制视图,我应该如何做?在这种情况下,您必须从子对象中删除单击侦听器,并将触摸侦听器添加到自定义布局。根据触摸位置,您可以识别单击的子对象。我使用dispathDraw方法解决了我的问题,因为onDraw方法将在子对象下绘制我的视图。