星空网 > 软件开发 > 操作系统

安卓自定义view(简单折线图)

直接上代码。。。。

public class CastView7 extends View{

    private Paint linePaint;//坐标系的直线画笔
    private Paint zhexianPaint;//折现画笔
    private Paint dianPaint;//点的画笔
    private Paint textPaint;//文字的画笔
    
    private int x=0;
    private int y=0;
    //用来装每个点的(x,y)
    private List<Map<String, Integer>> list=new ArrayList<Map<String,Integer>>();
    public final static String X="x";
    public final static String Y="y";
    public final static int DELL=50;
    public CastView7(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
        WindowManager wm =  (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
         //获取屏幕宽高
        x = wm.getDefaultDisplay().getWidth();
        y = wm.getDefaultDisplay().getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(DELL,DELL,DELL,y-250, linePaint);//画X的正坐标线
        int county=(x-250)/DELL;
        for(int i=0;i<county/2;i++){    //循环画出横向的所有线和数字
            canvas.drawLine(DELL,(y-250)-(DELL*i),x-DELL, (y-250)-(DELL*i), linePaint);
            canvas.drawText(i+"", 30,(y-250)-(DELL*i), textPaint);
        }
        int countx=x/DELL;
        for(int i=0;i<countx/2;i++){//画Y轴的数字
            canvas.drawText(i+"", DELL+(100*i),y-220, textPaint);
        }
        
        for(int i=0;i<list.size();i++){//折现图和拐点
            if(i>0)
                canvas.drawLine(list.get(i-1).get(X), list.get(i-1).get(Y), list.get(i).get(X), list.get(i).get(Y), zhexianPaint);
                canvas.drawCircle(list.get(i).get(X), list.get(i).get(Y), 5, dianPaint);
        }
        
    }
    private void init(){
        linePaint=new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.BLACK);
        
        zhexianPaint=new Paint();
        zhexianPaint.setAntiAlias(true);
        zhexianPaint.setColor(Color.RED);
        
        dianPaint=new Paint();
        dianPaint.setAntiAlias(true);
        dianPaint.setStyle(Style.FILL);
        dianPaint.setColor(Color.BLUE);
        
        textPaint=new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setAntiAlias(true);
        
    }
    //留给activity调用的方法,重绘。
    public void setList(int x,int y){
        Map<String, Integer> map=new HashMap<String, Integer>();
        map.put(X, x);
        map.put(Y, y);
        list.add(map);
        invalidate();
        
    }
}

 

Activity的代码:

public class Activity7 extends Activity{

    private CastView7 cv7;
    private int x=0;
    private int y=0;
    private Handler h=new Handler(){
        public void handleMessage(android.os.Message msg) {
            cv7.setList(msg.arg1, msg.arg2);//调用setlist重绘view
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity7);
        cv7=(CastView7) findViewById(R.id.cv7);
        WindowManager wm =  (WindowManager)this
                .getSystemService(Context.WINDOW_SERVICE);
        
        x = wm.getDefaultDisplay().getWidth();
        y = wm.getDefaultDisplay().getHeight();
        
        new Thread(){
            public void run() {
                int countx=x/50;Log.i("zhangteng",countx/2+"");
                for(int i=0;i<countx/2;i++){
                    int xx=50+(100*i);
                    int yy=((y-250)-(50*((int)(Math.random()*16))));
                    Message msg=new Message();
                    msg.what=1;
                    msg.arg1=xx;
                    msg.arg2=yy;
                    h.sendMessage(msg);
                    try {
                        sleep(1000);//一秒画一条
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                }
            };
        }.start();
    }
    
}

 

布局文件:

<?<LinearLayout     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.zhangteng.castomview7.CastView7
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cv7"
        />

</LinearLayout>

 

最后别忘了设置清单文件,把手机设置成为横屏模式。




原标题:安卓自定义view(简单折线图)

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

国际版tiktok安卓:https://www.goluckyvip.com/tag/82419.html
tiktok安卓下载破解版:https://www.goluckyvip.com/tag/82420.html
tiktok日版下载:https://www.goluckyvip.com/tag/82421.html
tiktok国际版pc端下载:https://www.goluckyvip.com/tag/82422.html
tiktok的url链接:https://www.goluckyvip.com/tag/82423.html
tiktok福利关键词:https://www.goluckyvip.com/tag/82425.html
怎样做出一个有利可图的SaaS产品?:https://www.kjdsnews.com/a/1836639.html
【再放信号】美国Etsy即将放开中国卖家入驻,官方邮件你收到了吗?:https://www.kjdsnews.com/a/1836640.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流