转载:
http://www.eoeandroid.com/thread-329148-1-1.html
package com.example.demo3;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
public class BPUtil {
private static final char[] CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static BPUtil bpUtil;
public static BPUtil getInstance() {
if(bpUtil == null)
bpUtil = new BPUtil();
return bpUtil;
}
// width="60" height="30"
// base_padding_left="5"
// range_padding_left="10"
// base_padding_top="15"
// range_padding_top="10"
// codeLength="4"
// line_number="3"
// font_size="20"
//default settings
private static final int DEFAULT_CODE_LENGTH =5;
private static final int DEFAULT_FONT_SIZE = 45;
private static final int DEFAULT_LINE_NUMBER = 1;
private static final int BASE_PADDING_LEFT = 25,
RANGE_PADDING_LEFT =30, BASE_PADDING_TOP =30,
RANGE_PADDING_TOP = 25;
private static final int DEFAULT_WIDTH = 300, DEFAULT_HEIGHT =80;
//settings decided by the layout xml
//canvas width and height
private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
//random word space and pading_top
private int base_padding_left = BASE_PADDING_LEFT,
range_padding_left = RANGE_PADDING_LEFT,
base_padding_top = BASE_PADDING_TOP,
range_padding_top = RANGE_PADDING_TOP;
//number of chars, lines; font size
private int codeLength = DEFAULT_CODE_LENGTH,
line_number = DEFAULT_LINE_NUMBER,
font_size = DEFAULT_FONT_SIZE;
//variables
private String code;
private int padding_left, padding_top;
private Random random = new Random();
public Bitmap createBitmap() {
padding_left = 0;
Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas c = new Canvas(bp);
code = createCode();
c.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setTextSize(font_size);
for (int i = 0; i < code.length(); i++) {
randomTextStyle(paint);
randomPadding();
c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
}
for (int i = 0; i < line_number; i++) {
drawLine(c, paint);
}
c.save( Canvas.ALL_SAVE_FLAG );//保存
c.restore();//
return bp;
}
public String getCode() {
return code;
}
private String createCode() {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < codeLength; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
private void drawLine(Canvas canvas, Paint paint) {
int color = randomColor();
int startX = random.nextInt(width);
int startY = random.nextInt(height);
int stopX = random.nextInt(width);
int stopY = random.nextInt(height);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
private int randomColor() {
return randomColor(1);
}
private int randomColor(int rate) {
int red = random.nextInt(256) / rate;
int green = random.nextInt(256) / rate;
int blue = random.nextInt(256) / rate;
return Color.rgb(red, green, blue);
}
private void randomTextStyle(Paint paint) {
int color = randomColor();
paint.setColor(color);
//paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体
paint.setFakeBoldText(true);
float skewX = random.nextInt(11) / 10;
skewX = random.nextBoolean() ? skewX : -skewX;
paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
// paint.setUnderlineText(true); //true为下划线,false为非下划线
// paint.setStrikeThruText(true); //true为删除线,false为非删除线
}
private void randomPadding() {
padding_left += base_padding_left + random.nextInt(range_padding_left);
padding_top = base_padding_top + random.nextInt(range_padding_top);
}
}
使用方法:
imageView.setImageBitmap(bpUtil.getInstance().createBitmap());
sss=bpUtil.getInstance().getCode();
刷新验证码:
imageView.setImageBitmap(bpUtil.getInstance().createBitmap());
sss=bpUtil.getInstance().getCode();
个人看法:
1.
randomColor(int rate) {
}
rate可以直接去掉
2.
可以考虑做成自定义view,代码是个工具类,宽高写死,没有适配的功能~
转载:
http://www.eoeandroid.com/thread-550328-1-1.html
将一个Android工程作为library工程需要注意的内容:
1.library工程不能引用其他的library工程
2.library工程中不能有switch,case,case的值是R.xx.xx,因为这是的R文件中的值已经不是final类型的了,需要改成if,else
3.library工程中不能有assert,raw,因为访问不到了,但是可以将这些下面发到主工程下,你会发现神奇的一幕产生,它可以访问了
4.library工程中不能有aidl文件
5.library工程不能和主程序有相同名称的Layout,string,id,drawable,等等R下有定义的,否则会被主程序覆盖掉
6.library工程的所有Activity、Service、receiver、provider,还要添加library工程的permission、uses-library等属性,注意引用的组件要使用完全的包名,否则将报对应的NotFoundException。
7.library的sdk版本不能高于主程序,否则编译不通过。
(本人编码不太想要library,容易报错)
Canvas绘制文本沿着path绘制(文本会弯)
@Override
protected void onDraw(Canvas canvas) {
Path path = new Path(); //定义一条路径
path.moveTo(10,10); //移动到坐标10,10
path.lineTo(50,60);
path.lineTo(200,80);
path.lineTo(10, 10);
canvas.drawPath(path, paint);
canvas.drawTextOnPath("Android777开发者博客",
path, 10, 10, paint);
}
画跳到的心性
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Love extends SurfaceView implements SurfaceHolder.Callback,
Runnable {
boolean mbloop = false;
SurfaceHolder mSurfaceHolder = null;
private Canvas canvas;
int miCount = 0;
int y = 50;
public Love(Context context) {
super(context);
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
this.setFocusable(true);
this.setKeepScreenOn(true);
mbloop = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
new Thread(this).start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mbloop = false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (mbloop) {
try {
Thread.sleep(200);
} catch (Exception e) {
// TODO: handle exception
}
synchronized (mSurfaceHolder) {
Draw();
}
}
}
private void Draw() {
// TODO Auto-generated method stub
canvas = mSurfaceHolder.lockCanvas();
try {
if (mSurfaceHolder == null || canvas == null) {
return;
}
if (miCount < 100) {
miCount++;
} else {
miCount = 0;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawRect(0, 0, 320, 480, paint);
switch (miCount % 6) {
case 0:
paint.setColor(Color.BLUE);
break;
case 1:
paint.setColor(Color.GREEN);
break;
case 2:
paint.setColor(Color.RED);
break;
case 3:
paint.setColor(Color.YELLOW);
break;
case 4:
paint.setColor(Color.argb(255, 255, 181, 216));
break;
case 5:
paint.setColor(Color.argb(255, 0, 255, 255));
break;
default:
paint.setColor(Color.WHITE);
break;
}
int i, j;
double x, y, r;
for (i = 0; i <= 90; i++) {
for (j = 0; j <= 90; j++) {
r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j))
* 20;
x = r * Math.cos(Math.PI / 45 * j)
* Math.sin(Math.PI / 45 * i) + 320 / 2;
y = -r * Math.sin(Math.PI / 45 * j) + 400 / 4;
canvas.drawPoint((float) x, (float) y, paint);
}
}
paint.setTextSize(32);
paint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
RectF rect = new RectF(60, 400, 260, 405);
canvas.drawRoundRect(rect, (float) 1.0, (float) 1.0, paint);
canvas.drawText("Loving You", 75, 400, paint);
mSurfaceHolder.unlockCanvasAndPost(canvas);
} catch (Exception e) {
}
}
}[/mw_shl_code]
[mw_shl_code=java,true]package com.example.love;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Love love = new Love(this);
setContentView(love);
}
}[/mw_shl_code]