android动态添加图层

结果如图

draw_demo_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical">
    <FrameLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="0dip" 
        android:id="@+id/sc" android:scrollbars="horizontal"  
        android:layout_weight="1.0">
        <ImageView android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:src="@drawable/image0"
            />
    	</FrameLayout>
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Draw"
        android:onClick="drawHandler"/>
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:text="Clear"
        android:onClick="clearHandler"/>     
</LinearLayout>
DrawView.java

package xyq.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class DrawView extends View {

    public DrawView(Context context) {
        super(context);
        setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT
            ,LinearLayout.LayoutParams.FILL_PARENT)); 
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setColor(Color.RED);
        p.setStrokeWidth(5);
        canvas.drawLine(50, 50, 100, 150, p);
        canvas.drawLine(100, 150, 20, 50, p);
    }
}
MytesActivity.java

package xyq.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

public class MytesActivity extends Activity {
	private FrameLayout container;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_demo_layout);

        container = (FrameLayout)findViewById(R.id.sc);        
        container.addView(new DrawView(this));
        Log.v("test", ""+container.getChildCount());
        
    }

    public void drawHandler(View target){
        container.addView(new DrawView(this));
        Log.v("test", ""+container.getChildCount());
    }

    public void clearHandler(View target){
        if(container.getChildCount() != 1){
            container.removeViewAt(container.getChildCount()-1);
        }
    }
}

源代码

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值