Skip to content

Instantly share code, notes, and snippets.

@mohamad-shafaee
Created August 5, 2019 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamad-shafaee/4393d09bc8b7229b0c6d6b491f2b00b9 to your computer and use it in GitHub Desktop.
Save mohamad-shafaee/4393d09bc8b7229b0c6d6b491f2b00b9 to your computer and use it in GitHub Desktop.
public class ImageBoard extends View {
RectF mCurrentViewPort;
Rect mContentRect;
ImageView image;
int width;
int height;
int dragedWidth = 0;
int dragedHeight = 0;
int dx = 0;
int dy = 0;
Paint boarderPaint;
int drawableWidth;
int drawableHeight;
Drawable src;
//GestureDetector gd;
GestureDetectorCompat gdc;
public ImageBoard(Context context){
//this(context, null, 0);
super(context);
}
public ImageBoard(Context context, AttributeSet attrs){
//this(context, attrs, 0);
super(context, attrs);
init(context, attrs);
}
public ImageBoard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ImageBoard(Context context, @Nullable AttributeSet attrs,
int defStyleAttr, int defStyleRes){
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int desiredWidth = 300;
int desiredHeight = 300;
int width;
int height;
//width
if(widthMode == MeasureSpec.EXACTLY){
//Must be this size
width = widthSize;
}else if(widthMode == MeasureSpec.AT_MOST){
//Can't be bigger than...
width = Math.min(widthSize, desiredWidth);
}else{
//Be whatever you want
width = desiredWidth;
}
//height
if(heightMode == MeasureSpec.EXACTLY){
//Must be this size
height = heightSize;
}else if(heightMode == MeasureSpec.AT_MOST){
//Can't be bigger than...
height = Math.min(heightSize, desiredHeight);
}else{
//Be whatever you want
height = desiredHeight;
}
//MUST CALL THIS
setMeasuredDimension(width, height);
}
public void init(Context context, @Nullable AttributeSet attrs){
//gd = new GestureDetector(context, mGestureListener);
gdc = new GestureDetectorCompat(context, new MyGestureListener());
mCurrentViewPort = new RectF();
mContentRect = new Rect();
boarderPaint = new Paint();
boarderPaint.setColor(Color.BLUE);
boarderPaint.setStyle(Paint.Style.STROKE);
boarderPaint.setStrokeWidth(20);
TypedArray ta = context.
obtainStyledAttributes(attrs, R.styleable.ImageBoard, 0, 0);
try{
src = (Drawable) ta.getDrawable(R.styleable.ImageBoard_src);
drawableWidth = src.getIntrinsicWidth();
drawableHeight = src.getIntrinsicHeight();
}catch(Exception ex){
Toast.makeText(context, "Error: " + ex.toString(),
Toast.LENGTH_LONG + 3000).show();
}
finally {
ta.recycle();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mContentRect.left = (int) dx;
mContentRect.top = (int) dy;
mContentRect.right = (int) dx + drawableWidth;
mContentRect.bottom = (int) dy + drawableHeight;
mCurrentViewPort.left = 0;
mCurrentViewPort.top = 0;
mCurrentViewPort.right = width;
mCurrentViewPort.bottom = height;
if(src != null)
{
src.setBounds(mContentRect);
src.draw(canvas);
}
canvas.drawRect(mCurrentViewPort, boarderPaint);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onDown(MotionEvent e){
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY){
dx -= (int) distanceX;
dy -= (int) distanceY;
ImageBoard.this.invalidate();
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent e){
this.gdc.onTouchEvent(e);
// return super.onTouchEvent(e);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment