編輯:關於Android編程
package chroya.demo.magnifier;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.MotionEvent;
import android.view.View;
/**
* 放大鏡實現方式1
* @author chroya
*
*/
public class ShaderView extends View{
private Bitmap bitmap;
private ShapeDrawable drawable;
//放大鏡的半徑
private static final int RADIUS = 80;
//放大倍數
private static final int FACTOR = 3;
private Matrix matrix = new Matrix();
public ShaderView(Context context) {
super(context);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.show);
bitmap = bmp;
BitmapShader shader = new BitmapShader(
Bitmap.createScaledBitmap(bmp, bmp.getWidth()*FACTOR,
bmp.getHeight()*FACTOR, true), TileMode.CLAMP, TileMode.CLAMP);
//圓形的drawable
drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setShader(shader);
drawable.setBounds(0, 0, RADIUS*2, RADIUS*2);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
//這個位置表示的是,畫shader的起始位置
matrix.setTranslate(RADIUS-x*FACTOR, RADIUS-y*FACTOR);
drawable.getPaint().getShader().setLocalMatrix(matrix);
//bounds,就是那個圓的外切矩形
drawable.setBounds(x-RADIUS, y-RADIUS, x+RADIUS, y+RADIUS);
invalidate();
return true;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, 0, 0, null);
drawable.draw(canvas);
}
}
二:
package chroya.demo.magnifier;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.view.MotionEvent;
import android.view.View;
/**
* 放大鏡實現方式2
* @author chroya
*
*/
public class PathView extends View{
private Path mPath = new Path();
private Matrix matrix = new Matrix();
private Bitmap bitmap;
//放大鏡的半徑
private static final int RADIUS = 80;
//放大倍數
private static final int FACTOR = 2;
private int mCurrentX, mCurrentY;
public PathView(Context context) {
super(context);
mPath.addCircle(RADIUS, RADIUS, RADIUS, Direction.CW);
matrix.setScale(FACTOR, FACTOR);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.show);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mCurrentX = (int) event.getX();
mCurrentY = (int) event.getY();
invalidate();
return true;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//底圖
canvas.drawBitmap(bitmap, 0, 0, null);
//剪切
canvas.translate(mCurrentX - RADIUS, mCurrentY - RADIUS);
canvas.clipPath(mPath);
//畫放大後的圖
canvas.translate(RADIUS-mCurrentX*FACTOR, RADIUS-mCurrentY*FACTOR);
canvas.drawBitmap(bitmap, matrix, null);
}
}
Android Service 詳細介紹:1、Service的概念 2、Service的生命周期 3、實例:控制音樂播放的Service一、Service的概念Servi
最近一直都在看自定義View這一塊。差不多一個星期了吧。這個星期堅持每天更新博客,感覺自己的技術也有點突破,對自定義View的計算也有了更深的認識。今天看到手機一個成長天
效果預覽源碼在文章末尾。引文有時候我們的頁面內容過長的時候,滑動到頁面底部用戶再滑動到頂部很麻煩,Android不像iOS可以點擊statusBar回到頂部,一般都是雙擊
本文實例為大家分享了Android自定義下拉刷新上拉加載的具體實現步驟,供大家參考,具體內容如下實現的方式是SwipeRefreshLayout + RecyclerVi