当前位置: 编程技术>移动开发
本页文章导读:
▪[转帖]自动调整TextView字体大小以顺应文字长度 [转帖]自动调整TextView字体大小以适应文字长度
抱歉,具体出处忘记了
package com.test.android.textview;
import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widge.........
▪ 画夹使用 画板使用
Painting.h#import <UIKit/UIKit.h>
//CONSTANTS:
#define kRubberWidth 20
#define kBrushLineAlpha 1.0
#define kPaintViewBackGroudImg @"背景.png"
@interface Painting : UIView {
UIImageView *drawImage;.........
▪ Andorid上進行Log 記錄 Andorid下進行Log 記錄
詳細見http://developer.android.com/reference/android/util/Log.htmlsimple example:private static final String TAG = "MyActivity";Log.v(TAG, "index=" + i);
......
[1][转帖]自动调整TextView字体大小以顺应文字长度
来源: 互联网 发布时间: 2014-02-18
[转帖]自动调整TextView字体大小以适应文字长度
抱歉,具体出处忘记了
抱歉,具体出处忘记了
package com.test.android.textview;
import android.content.Context;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView {
private static float DEFAULT_MIN_TEXT_SIZE = 10;
private static float DEFAULT_MAX_TEXT_SIZE = 20;
// Attributes
private Paint testPaint;
private float minTextSize, maxTextSize;
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initialise();
}
private void initialise() {
testPaint = new Paint();
testPaint.set(this.getPaint());
// max size defaults to the intially specified text size unless it is
// too small
maxTextSize = this.getTextSize();
if (maxTextSize <= DEFAULT_MIN_TEXT_SIZE) {
maxTextSize = DEFAULT_MAX_TEXT_SIZE;
}
minTextSize = DEFAULT_MIN_TEXT_SIZE;
};
/**
* Re size the font so the specified text fits in the text box * assuming
* the text box is the specified width.
*/
private void refitText(String text, int textWidth) {
if (textWidth > 0) {
int availableWidth = textWidth - this.getPaddingLeft()
- this.getPaddingRight();
float trySize = maxTextSize;
testPaint.setTextSize(trySize);
while ((trySize > minTextSize)
&& (testPaint.measureText(text) > availableWidth)) {
trySize -= 1;
if (trySize <= minTextSize) {
trySize = minTextSize;
break;
}
testPaint.setTextSize(trySize);
}
this.setTextSize(trySize);
}
};
@Override
protected void onTextChanged(CharSequence text, int start, int before,
int after) {
super.onTextChanged(text, start, before, after);
refitText(text.toString(), this.getWidth());
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw) {
refitText(this.getText().toString(), w);
}
}
}
[2] 画夹使用
来源: 互联网 发布时间: 2014-02-18
画板使用
Painting.h
Painting.m
Painting.h
#import <UIKit/UIKit.h>
//CONSTANTS:
#define kRubberWidth 20
#define kBrushLineAlpha 1.0
#define kPaintViewBackGroudImg @"背景.png"
@interface Painting : UIView {
UIImageView *drawImage;
int mouseMoved;
BOOL mouseSwiped;
BOOL isRubber;
CGPoint lastPoint;
CGFloat kBrushRGBColorRed;
CGFloat kBrushRGBColorGreen;
CGFloat kBrushRGBColorBlue;
CGFloat kBrushLineWidth;
}
@property(nonatomic, readwrite) BOOL isRubber;
@property(nonatomic, readwrite) CGFloat kBrushLineWidth;
- (void)clear;
- (void)save;
//- (void)changRGBColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
- (void)setBrushColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
@endPainting.m
#import "Painting.h"
#import <QuartzCore/QuartzCore.h>
@implementation Painting
@synthesize isRubber;
@synthesize kBrushLineWidth;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
drawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kPaintViewBackGroudImg]];
drawImage.frame = self.frame;
[self addSubview:backGroudImage];
[self addSubview:drawImage];
mouseMoved = 0;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
//双击清空
//if ([touch tapCount] == 2) {
// [self clear];
//}
lastPoint = [touch locationInView:self];
//lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
//currentPoint.y -= 20; // only for 'kCGLineCapRound'
UIGraphicsBeginImageContext(self.frame.size);
//Albert Renshaw - Apps4Life
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kBrushLineWidth); // for size 线条宽度
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), kBrushRGBColorRed, kBrushRGBColorGreen, kBrushRGBColorBlue, kBrushLineAlpha); //values for R, G, B, and Alpha
//CGContextSetLineJoin(UIGraphicsGetCurrentContext() , kCGLineJoinRound );
if (isRubber) {
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kRubberWidth); // for size 线条宽度
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);//混合模式
// CGContextClearRect(UIGraphicsGetCurrentContext(),CGRectMake(currentPoint.x - kRubberWidth/2, currentPoint.y - kRubberWidth/2,kRubberWidth,kRubberWidth));
}
// else {
// CGContextBeginPath(UIGraphicsGetCurrentContext());
// CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
// CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
// }
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//UITouch *touch = [touches anyObject];
//双击清空
//if ([touch tapCount] == 2) {
// [self clear];
//}
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), kBrushLineWidth);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), kBrushRGBColorRed, kBrushRGBColorGreen, kBrushRGBColorBlue, kBrushLineAlpha);
//CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
if (isRubber) {
CGContextClearRect(UIGraphicsGetCurrentContext(),CGRectMake(lastPoint.x - kRubberWidth/2, lastPoint.y - kRubberWidth/2,kRubberWidth,kRubberWidth));
}else {
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
}
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void) clear{
[drawImage setImage:[UIImage imageNamed:kPaintViewBackGroudImg]];
}
- (void)save{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
//UIImage *viewImage = [UIImage imageNamed:@"pink2.png"];
UIGraphicsEndImageContext();
if (viewImage != nil) {
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存完毕"
message:@"保存到图片浏览目录中"
delegate:nil
cancelButtonTitle:@"关闭"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)setBrushColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue
{
kBrushRGBColorRed = red;
kBrushRGBColorGreen = green;
kBrushRGBColorBlue = blue;
}
- (void)dealloc {
[super dealloc];
}
@end
[3] Andorid上進行Log 記錄
来源: 互联网 发布时间: 2014-02-18
Andorid下進行Log 記錄
詳細見
http://developer.android.com/reference/android/util/Log.html
simple example:
private static final String TAG = "MyActivity";
Log.v(TAG, "index=" + i);
詳細見
http://developer.android.com/reference/android/util/Log.html
simple example:
private static final String TAG = "MyActivity";
Log.v(TAG, "index=" + i);
最新技术文章: