use of android.graphics.drawable.Drawable in project AndroidPdfViewer by barteksc.
the class PDFView method onDraw.
@Override
protected void onDraw(Canvas canvas) {
if (isInEditMode()) {
return;
}
// As I said in this class javadoc, we can think of this canvas as a huge
// strip on which we draw all the images. We actually only draw the rendered
// parts, of course, but we render them in the place they belong in this huge
// strip.
// That's where Canvas.translate(x, y) becomes very helpful.
// This is the situation :
// _______________________________________________
// | | |
// | the actual | The big strip |
// | canvas | |
// |_____________| |
// |_______________________________________________|
//
// If the rendered part is on the bottom right corner of the strip
// we can draw it but we won't see it because the canvas is not big enough.
// But if we call translate(-X, -Y) on the canvas just before drawing the object :
// _______________________________________________
// | _____________|
// | The big strip | |
// | | the actual |
// | | canvas |
// |_________________________________|_____________|
//
// The object will be on the canvas.
// This technique is massively used in this method, and allows
// abstraction of the screen position when rendering the parts.
// Draws background
Drawable bg = getBackground();
if (bg == null) {
canvas.drawColor(Color.WHITE);
} else {
bg.draw(canvas);
}
if (recycled) {
return;
}
if (state != State.SHOWN) {
return;
}
// Moves the canvas before drawing any element
float currentXOffset = this.currentXOffset;
float currentYOffset = this.currentYOffset;
canvas.translate(currentXOffset, currentYOffset);
// Draws thumbnails
for (PagePart part : cacheManager.getThumbnails()) {
drawPart(canvas, part);
}
// Draws parts
for (PagePart part : cacheManager.getPageParts()) {
drawPart(canvas, part);
}
// Draws the user layer
if (onDrawListener != null) {
canvas.translate(toCurrentScale(currentFilteredPage * optimalPageWidth), 0);
//
onDrawListener.onLayerDrawn(//
canvas, //
toCurrentScale(optimalPageWidth), toCurrentScale(optimalPageHeight), currentPage);
canvas.translate(-toCurrentScale(currentFilteredPage * optimalPageWidth), 0);
}
// Restores the canvas position
canvas.translate(-currentXOffset, -currentYOffset);
}
use of android.graphics.drawable.Drawable in project PhotoView by bm-x.
the class PhotoView method setImageResource.
@Override
public void setImageResource(int resId) {
Drawable drawable = null;
try {
drawable = getResources().getDrawable(resId);
} catch (Exception e) {
}
setImageDrawable(drawable);
}
use of android.graphics.drawable.Drawable in project PhotoView by bm-x.
the class PhotoView method resetBase.
private void resetBase() {
Drawable img = getDrawable();
int imgw = getDrawableWidth(img);
int imgh = getDrawableHeight(img);
mBaseRect.set(0, 0, imgw, imgh);
mBaseMatrix.set(mSynthesisMatrix);
mBaseMatrix.mapRect(mBaseRect);
mHalfBaseRectWidth = mBaseRect.width() / 2;
mHalfBaseRectHeight = mBaseRect.height() / 2;
mScale = 1;
mTranslateX = 0;
mTranslateY = 0;
mAnimaMatrix.reset();
}
use of android.graphics.drawable.Drawable in project PhotoView by bm-x.
the class PhotoView method onMeasure.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!hasDrawable) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
Drawable d = getDrawable();
int drawableW = getDrawableWidth(d);
int drawableH = getDrawableHeight(d);
int pWidth = MeasureSpec.getSize(widthMeasureSpec);
int pHeight = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = 0;
int height = 0;
ViewGroup.LayoutParams p = getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
if (p.width == ViewGroup.LayoutParams.MATCH_PARENT) {
if (widthMode == MeasureSpec.UNSPECIFIED) {
width = drawableW;
} else {
width = pWidth;
}
} else {
if (widthMode == MeasureSpec.EXACTLY) {
width = pWidth;
} else if (widthMode == MeasureSpec.AT_MOST) {
width = drawableW > pWidth ? pWidth : drawableW;
} else {
width = drawableW;
}
}
if (p.height == ViewGroup.LayoutParams.MATCH_PARENT) {
if (heightMode == MeasureSpec.UNSPECIFIED) {
height = drawableH;
} else {
height = pHeight;
}
} else {
if (heightMode == MeasureSpec.EXACTLY) {
height = pHeight;
} else if (heightMode == MeasureSpec.AT_MOST) {
height = drawableH > pHeight ? pHeight : drawableH;
} else {
height = drawableH;
}
}
if (mAdjustViewBounds && (float) drawableW / drawableH != (float) width / height) {
float hScale = (float) height / drawableH;
float wScale = (float) width / drawableW;
float scale = hScale < wScale ? hScale : wScale;
width = p.width == ViewGroup.LayoutParams.MATCH_PARENT ? width : (int) (drawableW * scale);
height = p.height == ViewGroup.LayoutParams.MATCH_PARENT ? height : (int) (drawableH * scale);
}
setMeasuredDimension(width, height);
}
use of android.graphics.drawable.Drawable in project PhotoView by bm-x.
the class PhotoView method initBase.
private void initBase() {
if (!hasDrawable)
return;
if (!isKnowSize)
return;
mBaseMatrix.reset();
mAnimaMatrix.reset();
isZoonUp = false;
Drawable img = getDrawable();
int w = getWidth();
int h = getHeight();
int imgw = getDrawableWidth(img);
int imgh = getDrawableHeight(img);
mBaseRect.set(0, 0, imgw, imgh);
// 以图片中心点居中位移
int tx = (w - imgw) / 2;
int ty = (h - imgh) / 2;
float sx = 1;
float sy = 1;
// 缩放,默认不超过屏幕大小
if (imgw > w) {
sx = (float) w / imgw;
}
if (imgh > h) {
sy = (float) h / imgh;
}
float scale = sx < sy ? sx : sy;
mBaseMatrix.reset();
mBaseMatrix.postTranslate(tx, ty);
mBaseMatrix.postScale(scale, scale, mScreenCenter.x, mScreenCenter.y);
mBaseMatrix.mapRect(mBaseRect);
mHalfBaseRectWidth = mBaseRect.width() / 2;
mHalfBaseRectHeight = mBaseRect.height() / 2;
mScaleCenter.set(mScreenCenter);
mRotateCenter.set(mScaleCenter);
executeTranslate();
switch(mScaleType) {
case CENTER:
initCenter();
break;
case CENTER_CROP:
initCenterCrop();
break;
case CENTER_INSIDE:
initCenterInside();
break;
case FIT_CENTER:
initFitCenter();
break;
case FIT_START:
initFitStart();
break;
case FIT_END:
initFitEnd();
break;
case FIT_XY:
initFitXY();
break;
}
isInit = true;
if (mFromInfo != null && System.currentTimeMillis() - mInfoTime < MAX_ANIM_FROM_WAITE) {
animaFrom(mFromInfo);
}
mFromInfo = null;
}
Aggregations