长图:
效果:
主要涉及:canvas.drawBitmap(Bitmap bitmap,Rect src,Rect dsc,Paint paint); 设计:使用drawBitmap 进行区域作画,每一次画长图的一个item到界面上。 设计点击事件,点击一次进行图片的更新,显示下一张图片,如果到最后一张,则重新开始显示 计算得,图片的宽高,将控件的宽高设置为小图片的宽高。
主要代码:
@OverridePRotected void onDraw(Canvas canvas) { super.onDraw(canvas); int left = currentPic * width; int top = 0; int right = (currentPic + 1) * width; int bottom = width; //图像选取 Rect src = new Rect(left, top, right, bottom); RectF dsc = new RectF(0, 0, width, height); canvas.drawBitmap(bitmap, src, dsc, null);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取图片的大小 int picHeight = bitmap.getHeight(); int picWidth = bitmap.getWidth(); width = picWidth / totalPic; height = picHeight; setMeasuredDimension(width, height);}@Overridepublic boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_UP) { //处理点击事件 if (currentPic == totalPic - 1) { currentPic = -1; } currentPic++; postInvalidate(); } return true;}代码: https://github.com/WhatWeCan/customer_views/tree/master/drawBitmap
新闻热点
疑难解答