网上有很多关于三个构造函数使用时机的说法,但是说法正确的却没有几家,这里正式的给大家科普一下:
创新互联建站是一家专注网站建设、网络营销策划、微信小程序、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立十余年以来,已经为上1000+成都发电机回收各业的企业公司提供互联网服务。现在,服务的上1000+客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。
详细可以看这个博客:
onMeasure方法是用来设置宽高的,当然也可以用来获取宽高,获取方法如下:
新手记得打印一下measureWidth,measureHeight。你会发现有时候measureWidth,measureHeight的值为0,所以当我们遇到0的时候不要使用。至于为什么会有0,我们下次再写博客讲解。
3.绘制一条直线
这里需要传五个参数,分别是
效果图如下
首先找到这条直线的所有点,然后开启一个线程循环获取点,用Handle发送消息去画线
@Override
public void run() {
try {
for(int i = 0 ; i myPointList.size(); i++)
{
MyPoint myPoint = myPointList1.get(i);
Message message = Message.obtain();
message.what = 2;
message.obj = myPoint;
mHandler.sendMessage(message);
Thread.sleep(100);
}
} catch (Exception e) {
e.printStackTrace();
}
}
你好!
步骤1:在你创建的工程中新建一个class文件,随便起个名字叫MyView吧,打开文件会看到:
public
class
MyView
{
}
步骤2:让该类继承
View类,也就是在MyView后面添几个字母,如:
public
class
MyView
extends
View
{
}
步骤3:在上面的这个类中,写一个方法:
public
class
MyView
extends
View
{
protected
void
onDraw(Canvas
canvas)
{
..........
}
}
步骤4:在onDrow方法中写一条语句:
public
class
MyView
extends
View
{
protected
void
onDraw(Canvas
canvas)
{
canvas.drawLine(起点横坐标,起点纵坐标,终点横坐标,终点纵坐标,线的颜色);
}
}
注:看到“drawLine”了吧,这就是在画线。
如有疑问,请追问。
public class MainActivity extends Activity {
private ImageView iv;
private Bitmap baseBitmap;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.iv = (ImageView) this.findViewById(R.id.iv);
// 创建一张空白图片
baseBitmap = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);
// 创建一张画布
canvas = new Canvas(baseBitmap);
// 画布背景为灰色
canvas.drawColor(Color.GRAY);
// 创建画笔
paint = new Paint();
// 画笔颜色为红色
paint.setColor(Color.RED);
// 宽度5个像素
paint.setStrokeWidth(5);
// 先将灰色背景画上
canvas.drawBitmap(baseBitmap, new Matrix(), paint);
iv.setImageBitmap(baseBitmap);
iv.setOnTouchListener(new OnTouchListener() {
int startX;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取手按下时的坐标
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 获取手移动后的坐标
int stopX = (int) event.getX();
int stopY = (int) event.getY();
// 在开始和结束坐标间画一条线
canvas.drawLine(startX, startY, stopX, stopY, paint);
// 实时更新开始坐标
startX = (int) event.getX();
startY = (int) event.getY();
iv.setImageBitmap(baseBitmap);
break;
}
return true;
}
});
}
public void save(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
OutputStream stream = new FileOutputStream(file);
baseBitmap.compress(CompressFormat.JPEG, 100, stream);
stream.close();
// 模拟一个广播,通知系统sdcard被挂载
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment
.getExternalStorageDirectory()));
sendBroadcast(intent);
Toast.makeText(this, "保存图片成功", 0).show();
} catch (Exception e) {
Toast.makeText(this, "保存图片失败", 0).show();
e.printStackTrace();
}
}
}
//代码如下
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;
public class BaiduMapActivity extends MapActivity {
private Context mContext;
private MapView mapView;
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private GeoPoint gpoint1, gpoint2, gpoint3;// 连线的点
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.baidumap_layout);
BaseApplication baseApp = (BaseApplication) this.getApplication();
if (baseApp.mBMapManage == null) {
baseApp.mBMapManage = new BMapManager(mContext);
baseApp.mBMapManage.init(baseApp.mStrKey,
new BaseApplication.MyGeneralListener());
}
baseApp.mBMapManage.start();
super.initMapActivity(baseApp.mBMapManage);// 初始化map sdk
mapView = (MapView) findViewById(R.id.bmapView);
mapView.setBuiltInZoomControls(true);
// 设置在缩放动画过程中也显示overlay,默认为不绘制
mapView.setDrawOverlayWhenZooming(true);
// RouteLine routeLine =
// (RouteLine)getIntent().getSerializableExtra("routeLine");
//这里画点和连接线
MyOverlay myOverlay = new MyOverlay();
mapView.getOverlays().add(myOverlay);
MapController mapController = mapView.getController();
mapController.zoomIn();
gpoint1 = new GeoPoint((int) (2259316 * 10),
(int) (11396279 * 10));
gpoint2 = new GeoPoint((int) (2259245 * 10),
(int) (11396226 * 10));
gpoint3 = new GeoPoint((int) (2259121 * 10),
(int) (11396066 * 10));
mapController.animateTo(gpoint1);//设置一个起点
}
class MyOverlay extends Overlay {
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
// 经度转像素
projection.toPixels(gpoint1, p1);
projection.toPixels(gpoint2, p2);
projection.toPixels(gpoint3, p3);
//第一个画笔 画圆
Paint fillPaint = new Paint();
fillPaint.setColor(Color.BLUE);
fillPaint.setAntiAlias(true);
fillPaint.setStyle(Style.FILL);
// 将图画到上层
canvas.drawCircle(p1.x, p1.y, 5.0f, fillPaint);
canvas.drawCircle(p2.x, p2.y, 5.0f, fillPaint);
canvas.drawCircle(p3.x, p3.y, 5.0f, fillPaint);
//第二个画笔 画线
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(4);
//连接
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
//画出路径
canvas.drawPath(path, paint);
}
}
}