Android实现纯java代码字体阴影效果,主要是通过activity的draw方法,进行重新绘制,如下代码:
创新互联服务项目包括江南网站建设、江南网站制作、江南网页制作以及江南网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,江南网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到江南省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
package canvas.test;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.View;
public class ShaderEffect extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ImageEffect(this));
}
class ImageEffect extends View{
Paint paint;
public ImageEffect (Context context){
super(context);
paint = new Paint();//初始化画笔,为后面阴影效果使用。
paint.setAntiAlias(true);//去除锯齿。
paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);//设置阴影层,这是关键。
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
int posX = 20;
int posY = 50;
int PicWidth,PicHegiht;
Drawable drawable = getResources().getDrawable(R.drawable.button);
Drawable dbe = getResources().getDrawable(R.drawable.button).mutate();//如果不调用mutate方法,则原图也会被改变,因为调用的资源是同一个,所有对象是共享状态的。
Drawable drawTest = getResources().getDrawable(R.drawable.button);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.button);
PicWidth = drawable.getIntrinsicWidth();
PicHegiht = drawable.getIntrinsicHeight();
drawTest.setBounds(posX, (2 * posY) + PicHegiht, posX + PicWidth, (2 * posY) + 2 * PicHegiht );
drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHegiht);
dbe.setBounds(0, 0, PicWidth, PicHegiht);
canvas.drawColor(Color.WHITE);//设置画布颜色
canvas.save(Canvas.MATRIX_SAVE_FLAG);
dbe.setColorFilter(0x7f000000,PorterDuff.Mode.SRC_IN);
canvas.translate(posX + (int)(0.9 * PicWidth/2), posY + PicHegiht/2);//图像平移为了刚好在原图后形成影子效果。
canvas.skew(-0.9F, 0.0F);//图像倾斜效果。
canvas.scale(1.0f, 0.5f);//图像(其实是画布)缩放,Y方向缩小为1/2。
dbe.draw(canvas);//此处为画原图像影子效果图,比原图先画,则会在下层。
drawable.clearColorFilter();
canvas.restore();
canvas.save(Canvas.MATRIX_SAVE_FLAG);
drawable.draw(canvas);//此处为画原图像,由于canvas有层次效果,因此会盖在影子之上。
canvas.restore();
//默认无效果原图
canvas.save(Canvas.MATRIX_SAVE_FLAG);
drawTest.draw(canvas);
canvas.restore();
//图片阴影效果
canvas.save(Canvas.MATRIX_SAVE_FLAG);
//Rect rect = new Rect(2*posX + PicWidth, 2*posY + PicHegiht, 2*posX + 2*PicWidth, 2*posY + 2*PicHegiht);//此为理论上的阴影图坐标
Rect rect = new Rect(2*posX + PicWidth + 3, 2*posY + PicHegiht + 3, 2*posX + 2*PicWidth - 2, 2*posY + 2*PicHegiht - 2);
//由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果
RectF rectF = new RectF(rect);
canvas.drawRoundRect(rectF, 10f, 10f, paint);//在原有矩形基础上,画成圆角矩形,同时带有阴影层。
canvas.drawBitmap(bmp, 2*posX + PicWidth, 2*posY + PicHegiht, null);//画上原图。
canvas.restore();
}
}
}
;
要制造那种效果只需要大约 30 行 Java 代码:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class RollingBall extends JPanel {
Ellipse2D.Float ball = new Ellipse2D.Float( -100, 100, 50, 50 );
public void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2 = ( Graphics2D ) g;
// Draw the ball
g2.fill( ball );
// Draw the rotating ellipse by skewing the Device Space
double angdeg = // One rotation per ball's travelling over its perimeter
ball.x++ % ( Math.PI * ball.width ) / ( Math.PI * ball.width ) * 360;
g2.rotate( Math.toRadians( angdeg ), ball.getCenterX( ), ball.getCenterY( ) );
g2.scale( .5, 1 );
g2.translate( ball.getCenterX( ), 0 );
g2.setColor( Color.gray );
g2.fill( ball );
}
public void roll( ) throws Exception {
while( true ) {
repaint( );
Thread.sleep( 8 );
}
}
public static void main( String[ ] args ) throws Exception {
JFrame f = new JFrame( );
RollingBall rb = new RollingBall( );
f.setSize( 999, 185 );
f.getContentPane( ).add( rb );
f.setVisible( true );
rb.roll( );
}
}
功能说明 代码实现了多种幻灯片变换特效 如 淡入淡出 缓慢覆盖 旋转覆盖等 多种变换效果
功能实现
图片加载类ImageLoader实现
)用阻塞队列存储要图片 BlockingQueue images = new ArrayBlockingQueue( )
)用图片eof表示图片队列结束 Image eof = new WritableImage( )
)循环读取指定图片 由于是阻塞队列 所以当队列满的时候线程会自动阻塞
public void run() {
int id = ;
try {
while (true) {
String path = resources[id];
InputStream is = getClass() getResourceAsStream(path)
if (is != null) {
Image image = new Image(is width height true true)
if (!image isError()) {
images put(image)
}
}
id++;
if (id = resources length) {
id = ;
}
}
} catch (Exception e) {
} finally {
if (!cancelled) {
try {
images put(eof)
} catch (InterruptedException e) {
}
}
}
}
特效实现 以弧形切换图片为例 首先定义LengthTransition变化特效 设置变化时间 以及弧度数跟时间的变化关系
class LengthTransition extends Transition {
Arc arc;
public LengthTransition(Duration d Arc arc) {
this arc = arc;
setCycleDuration(d)
}
@Override
protected void interpolate(double d) {
arc setLength(d * )
}
}
然后设置图片层叠效果
group setBlendMode(BlendMode SRC_OVER)
next setBlendMode(BlendMode SRC_ATOP)
以及之前那张图片的淡出特效
FadeTransition ft = new FadeTransition(Duration seconds( ) mask )
最后同时执行这两个特效
ParallelTransition pt = new ParallelTransition(lt ft)
效果图
public class ShakeFrame extends JFrame {
private JButton btn = new JButton("Click me!");
public ShakeFrame() {
super("抖动窗口");
this.setSize(300, 200);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
btn.setBounds(10, 10, 100, 30);
this.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x = ShakeFrame.this.getX();
int y = ShakeFrame.this.getY();
for (int i = 0; i 20; i++) {
if ((i 1) == 0) {
x += 3;
y += 3;
} else {
x -= 3;
y -= 3;
}
ShakeFrame.this.setLocation(x, y);
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
});
}
public static void main(String[] args) {
new ShakeFrame();
}
}