Problem with drawing canvas line in scrollView - java

I'm trying to draw a scrollable timeline. I implement singleLine custom view for drawing a line between circle ImageView's of recycler view:
public class SingleLine extends View {
WeakReference<Context> ctx;
Paint paint;
PointF pointA, pointB;
private int height_custom;
public SingleLine(Context context) {
super(context);
ctx = new WeakReference<>(context);
init();
}
public SingleLine(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
ctx = new WeakReference<>(context);
init();
}
public void init() {
paint = new Paint();
pointA = new PointF();
pointB = new PointF();
paint.setStrokeWidth(10);
paint.setColor(ctx.get().getResources().getColor(R.color.green));
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint);
invalidate();
}
public PointF getPointA() {return pointA;}
public void setPointA(PointF pointA) {this.pointA = pointA;}
public PointF getPointB() {return pointB;}
public void setPointB(PointF pointB) {this.pointB = pointB;}
}
And in onBindViewHolder() method of my adapter, I use an interface for measuring x/y coordinates of first and last visible view of recyclerview:
#Override
public void onBindViewHolder(#NonNull final TaskViewHolder holder, final int position) {
Tasks tasks = datalist_tasks.get(position);
String hour = "" + tasks.getHour() + ":" + tasks.getMinute();
holder.tv_clock.setText(hour);
holder.tv_title.setText(tasks.getTitle());
holder.tv_comment.setText(tasks.getComment());
holder.itemView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
holder.itemView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
holder.itemView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
if (iOnMeasureCustomView != null)
iOnMeasureCustomView.onMeasure(holder.iv_status, position);
}
});
}
And I implement that interface in my activity like below:
LinearLayoutManager layoutManager1 = new LinearLayoutManager(_C.get(), RecyclerView.VERTICAL, false);
rv_tasks.setLayoutManager(layoutManager1);
tasksAdapter.setiOnMeasureCustomView(new IOnMeasureCustomView() {
#Override
public void onMeasure(ImageView iv_status, int position) {
int[] screen = new int[2];
iv_status.getLocationOnScreen(screen);
int width = (int) ((iv_status.getWidth() / 2) + convertDpToPixel(8));
if (position == layoutManager1.findFirstCompletelyVisibleItemPosition()) {
pointA.set(iv_status.getX() + width, iv_status.getY() + convertDpToPixel(8));
}
else if (position == layoutManager1.findLastCompletelyVisibleItemPosition()) {
pointB.set(iv_status.getX() + width, screen[1] - convertDpToPixel(40));
singleLine.setPointA(pointA);
singleLine.setPointB(pointB);
singleLine.invalidate();
}
}
});
And also I implement addOnScrollListener() for my recyclerview and write this code snippet (I don't know that if this implementation is correct or no):
rv_tasks.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
PointF a = singleLine.getPointA();
PointF b = singleLine.getPointB();
if (dy > 0) {
a.y -= dy;
b.y -= dy;
singleLine.setPointA(a);
singleLine.setPointB(b);
singleLine.invalidate();
} else {
a.y += Math.abs(dy);
b.y += Math.abs(dy);
singleLine.setPointA(a);
singleLine.setPointB(b);
singleLine.invalidate();
}
}
});
My problem is when I scroll in activity, like the gif below, my singleLine is something like cutting off imageView and the end x/y of line become the x/y of end of the screen.
Questios
How can I set x/y of the line to prevent this?
And also I need some help with drawing this line with animation. How can I draw this line with animation that starts when the activity started?

Related

Android Circular Reveal Animation For Recyclerview Item Not Working

I want to circular reveal animation for recycler view items. I put the following code in the adapter class but it doesn't work. please help me.
public void anim(final CustomViewHolder customViewHolder) {
// get the center for the clipping circle
int centerX = (customViewHolder.imageView.getLeft() + customViewHolder.imageView.getRight()) / 2;
int centerY = (customViewHolder.imageView.getTop() + customViewHolder.imageView.getBottom()) / 2;
int startRadius = 0;
// get the final radius for the clipping circle
int endRadius = Math.max(customViewHolder.imageView.getWidth(), customViewHolder.imageView.getHeight());
// create the animator for this view (the start radius is zero)
Animator anim =
null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
anim = ViewAnimationUtils.createCircularReveal(customViewHolder.rv,
centerX, centerY, startRadius, endRadius);
anim.setDuration(1000);
// make the view invisible when the animation is done
anim.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
customViewHolder.rv.setVisibility(View.VISIBLE);
anim.start();
}
}

How to access point arraylist from another class?

When the activity is running, circles are drawn from an arraylist. When the user fling to the right , the point from the arraylist are saved into another arraylist(pointa). In another class, the arraylist(pointa) are supposed to be drawn as circles. However, it seems that it cannot get the x and y points of the arraylist(pointa). Can u help me identify where is the problem? Thank you.
public class EyeTestActivity extends AppCompatActivity {
int z = 0;
private GestureDetectorCompat mDetector;
TestView testview;
public ArrayList<Point> pointa;
public ArrayList<Point> pointlist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
testview = new TestView(this);
setContentView(testview);
// get the gesture detector
mDetector = new GestureDetectorCompat(EyeTestActivity.this, new SwipeGestureDetector(testview));
}
public boolean onTouchEvent(MotionEvent motionEvent) {
this.mDetector.onTouchEvent(motionEvent);
return super.onTouchEvent(motionEvent);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
super.dispatchTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}
public class TestView extends View {
Paint paint;
public TestView(Context context) {
super(context);
init();
setFocusable(true);
setFocusableInTouchMode(true);
createPointList();
}
public void init() {
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
}
public void createPointList() {
pointlist = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
float a = 100 * i;
float b = 100 * i;
for (int j = 1; j <= 24; j++) {
float x = (float) (a * Math.sin(Math.toRadians(15 * j)));
float y = (float) (b * Math.cos(Math.toRadians(15 * j)));
pointlist.add(new Point(x, y));
Log.i("new point " , x+", " +y);
//Add the x and y coordinates to the Point
}
}
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
canvas.drawColor(Color.BLACK);
Point point2 = pointlist.get(z);
canvas.drawCircle(point2.getX() + canvas.getWidth() / 2, point2.getY() + canvas.getHeight() / 2, 15, paint);
Log.i("onDraw " , " canvas drawing for z " + z + " at position " + point2.getX() + canvas.getWidth() / 2 + ", " + point2.getY() + canvas.getHeight() / 2);
}
}
public class SwipeGestureDetector implements GestureDetector.OnGestureListener {
ArrayList<Point> pointa = new ArrayList<Point>();
Intent intent = new Intent(EyeTestActivity.this, ResultExplanationActivity.class);
TestView testview;
public SwipeGestureDetector(TestView testview) {
this.testview = testview;
}
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public void onShowPress(MotionEvent e) {
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
#Override
public void onLongPress(MotionEvent e) {
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
testview.invalidate();
Log.i("onFling", " testview invalidate");
if (e1.getX() < e2.getX()) {
z++;
if (z >=120) {
finish();
startActivity(intent);
}
testview.invalidate();
Point point2 = pointlist.get(z);
pointa.add(new Point(point2.getX(), point2.getY()));
return true;
}
if (e1.getX() > e2.getX()) {
z++;
if (z >=120) {
finish();
startActivity(intent);
}
testview.invalidate();
return true;
}
if (e1.getY() < e2.getY()) {
}
if (e1.getY() > e2.getY()) {
}
return false;
}
}
public ArrayList<Point> getPointa() {
return this.pointa;
}
}
//another class( draw circles from pointa)
public class ResultExplanationActivity extends AppCompatActivity {
private EyeTestActivity eyetest;
ResultView resultView;
public ResultExplanationActivity() {
eyetest = new EyeTestActivity();
ArrayList<Point> pointa = eyetest.getPointa();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
resultView = new ResultExplanationActivity.ResultView(this);
setContentView(resultView);
ArrayList<Point> pointa = eyetest.getPointa();
// get the gesture detector
}
public class ResultView extends View {
ArrayList<Point> pointa = eyetest.getPointa();
Paint paint;
Paint painta;
public ResultView(Context context) {
super(context);
init();
setFocusable(true);
setFocusableInTouchMode(true);
}
public void init() {
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
painta = new Paint();
painta.setColor(Color.RED);
painta.setStrokeWidth(5);
painta.setStyle(Paint.Style.FILL);
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
canvas.drawColor(Color.BLACK);
for (int i=0;i<pointa.size();i++){
canvas.drawCircle(pointa.getX() + canvas.getWidth() / 2, pointa.getY() + canvas.getHeight() / 2, 10, paint);
}
}
}
}

Disable ViewPager Swipe From Right To Left and Left To Right Side

I am using ViewPager in one of my android application. I am learning yet Android and so does not know much about it. I want disable swipe from left to right and right to left on position based. I have searched in stackoverflow for solution but not got any proper solution. I am attaching my code here. Let me know if someone can help me to solve this puzzle.
My ViewPager
adapter = new MyAdapter(getSupportFragmentManager());
viewPager.setOffscreenPageLimit(0);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
constant.new_ad_count = constant.new_ad_count + 1;
mItemIndx = position;
try {
int index = position % ITEMS_PER_PAGE;
currentQuote = quotes.get(index);
} catch
(Exception e) {
}
mPageIndx = ((position + 1) / ITEMS_PER_PAGE) + (((position + 1) % ITEMS_PER_PAGE) > 0 ? 1 : 0);
updateUI();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(mItemIndx);
updateUI();
}
Function From Which I want Disable Swipe
private void updateUI() {
lblLikeCount.setText(currentQuote.getQuLike());
lblShareCount.setText(currentQuote.getQuShare());
lblTime.setText(currentQuote.getQuTime());
textPageCount.setText((mItemIndx + 1) + " / " + totalSize);
// butNext.setVisibility((mItemIndx < totalSize - 1) ? View.VISIBLE : View.INVISIBLE);
butPrev.setVisibility((mItemIndx > 0) ? View.VISIBLE : View.INVISIBLE);
if(mItemIndx==totalSize-1||(mItemIndx+1)%ITEMS_PER_PAGE==0) {
butNext.setVisibility(View.INVISIBLE);
//I want Disable Right to Left Swipe Here
} else {
butNext.setVisibility(View.VISIBLE);
}
if (mItemIndx-1<0 ||(mItemIndx)%ITEMS_PER_PAGE==0) {
butPrev.setVisibility(View.INVISIBLE);
//I want Disable Left to Right Here
} else {
butPrev.setVisibility(View.VISIBLE);
}
}
Thanks a lot Friends :)
Use a this NonSwipableViewPager class, using this, you will only be able to change the page when calling setCurrentItem.
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
setMyScroller();
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setMyScroller();
}
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
//down one is added for smooth scrolling
private void setMyScroller() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyScroller extends Scroller {
public MyScroller(Context context) {
super(context, new DecelerateInterpolator());
}
#Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
}
}
Add this class to your project, then in the layout where you want it, replace ViewPager with:
<yourpackagename.NonSwipeableViewPager
Then initialise it in your Activity or Fragment:
NonSwipeableViewPager pager = (NonSwipeableViewPager) findViewById(R.id.pager);

how to Get coordinates from Custom view android

I have some custom view with onTouchEvent where I'm setting mX and mY .
mx=event.getX();
my=event.getY();
this is for getting the drawing path of touch event - working great, and I can
see the coordinates in my text view which is defined in the main activity.
I want to use this 2 attributes in my Main activity layout
for setting the background color
for example
if (mx > 1000 && my > 100 ){
set color
}
but it seems that mx and my are not getting any values
what i tried is
custom_view paintview = (custom_view) findViewById(R.id.custum_view_id)
float mx =paintview.getmx();
float my =paintview.getmy();
maybe i didn't defined the getmx correctly in custom view ?
public float getmx(){
return mx;
}
i'm new in Java and android so be easy with me :)
adding MainActiviy.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Drawing - custom view
drawView = (Drawing) findViewById(R.id.drawing);
findCoordinates();
TextView Coordinates = (TextView) findViewById(R.id.CNcoordinates);
//Coordinates - message with coordinates
paintView.setTextView(Coordinates);
}
private Drawing drawView;
public void findCoordinates() {
Drawing paintView = (Drawing) findViewById(R.id.drawing);
float xX = drawView.getmX();
float yY = drawView.getmY();
int nColor = Color.BLUE;
View buttonmove1 = findViewById(R.id.layoutMove);
if (Math.abs(xX) > 1000 && Math.abs(yY) > 1000) {
buttonmove1.setBackgroundColor(nColor);
}
}
the coordinates come from:
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mX = event.getX();
mY = event.getY();
View buttonmove = findViewById(R.id.drawing);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(mX, mY);
if(myCoordinates!=null){
myCoordinates.setText(":" + mX + " , " + ":" + mY);
}
break;
public void setTextView(TextView tv){
myCoordinates = tv;
}
Use interface or callback after finishing your touchEvent. you have to re-call your
findCoordinates()
so it will update your UI respect to new values.
i succeed to use OnViewTouchListener as a callback:
adding the method in custom view:
private OnViewTouchListener OnViewTouchListener;
public interface OnViewTouchListener {
public void onViewTouched (float x, float y, boolean touched);
}
public void setOnViewTouchListener (OnViewTouchListener listener){
OnViewTouchListener = listener;
}

When i add a bitmap to an array list the last element is duplicated in previous indexes

I'm trying to implement a personal way of undo/redo in a finger paint-like app.
I have in synthesis three objects: the Main class (named ScorePadActivity), the relative Main Layout (with buttons, menus, etc, as well as a View object where I create my drawings), and a third object named ArrayList where i'm writing the undo/redo code.
The problem is, when I press the undo button nothing happens, but if I draw anything again "one-time" and press undo, the screen is updated. If I draw many times, to see any changes happen on screen I have to press the undo button the same number of times I have drawn.
Seems like (as in title) when I add a bitmap to the array list the last element is duplicated in previous indexes, and for some strange reason, everytime I press the Undo Button, the system is ok for one time, but starts to duplicate until the next undo.
The index increase is verified with a series of System.out.println inserted in code.
Now when I draw something on screen, the array list is updated with the code inserted after the invocation of touchup(); method in motionEvent
touch_up(); }
this.arrayClass.incrementArray(mBitmap);
mPath.rewind();
invalidate();
and in ArrayList activity;
public void incrementArray(Bitmap mBitmap) {
this._mBitmap=mBitmap;
_size=undoArray.size();
undoArray.add(_size, _mBitmap);
}
(All Logs removed for clear reading)
The undo button in ScorePadActivity calls the undo method in View activity:
Button undobtn= (Button)findViewById(R.id.undo);
undobtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mView.undo();
}
});
in View activity:
public void undo() {
this.mBitmap= arrayClass.undo();
mCanvas = new Canvas(mBitmap);
mPath.rewind();
invalidate();
}
that calls the relative undo method in ArrayList activity:
public Bitmap undo() {
// TODO Auto-generated method stub
_size=undoArray.size();
if (_size>1) {
undoArray.remove(_size-1);
_size=undoArray.size();
_mBitmap = ((Bitmap) undoArray.get(_size-1)).copy(Bitmap.Config.ARGB_8888,true);
}
return _mBitmap;
}
return mBitmap and invalidate:
Due to my bad English I have made a scheme to make the problem more clear:
I have tried with HashMap, with a simple array, I have tried to change mPath.rewind(); with reset();, new Path(); etc but nothing.
Why?
Sorry for the complex answer, i want give you a great thanks in advance.
Best regards
Edit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
some other layouts nested an buttons to form a upper toolbar
</LinearLayout>
<RelativeLayout
android:id="#+id/viewlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/buttonlayout"
android:background="#drawable/desk_wood" >
<com.example.android.touchexample.MyView
android:id="#+id/viewout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
</RelativeLayout>
This the Main Activity ScorePadActivity
public class ScorePadActivity extends Activity {
MyView mView;
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mView = (MyView)findViewById(R.id.viewout);
Button undobtn= (Button)findViewById(R.id.undo);
undobtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mView.undo();
}
});
This is the View Activity:
public class MyView extends View {
MyView myView;
Context context;
final ArrayClass arrayClass= new ArrayClass();
private Bitmap mBitmap;
private Bitmap savedBmp;
private static Canvas mCanvas;
private static Path mPath;
private static Paint mPaint;
/*
* some other variables here
*/
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(bSize);
mPaint.setAlpha(255);
mPath = new Path();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap((int) bmWidth, (int) bmHeight,Bitmap. Config.ARGB_8888);}
/*
* here add a blank bitmap at the start of the array at index 0
*/
arrayClass.incrementArray(mBitmap);
mCanvas = new Canvas(mBitmap);
}
public void onDraw(Canvas canvas) {
canvas.save();
bx= (((width/mScaleFactor)-width)/2)+center;
by= ((height/mScaleFactor)-height)/2;
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
canvas.drawBitmap(penta, bx, by, null);
mCanvas.drawPath(mPath, mPaint);
canvas.drawBitmap(mBitmap, bx, by, null);
lastmPosX=mPosX;
lastmPosY=mPosY;
lastmScaleFactor=mScaleFactor;
canvas.restore();
}
private void touch_start(float x, float y) {
x=((x/mScaleFactor)-bx)-(mPosX/mScaleFactor);
y=((y/mScaleFactor)-by)-(mPosY/mScaleFactor);
mPath.rewind();
mPath.moveTo(x, y);
move=false;
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
x=((x/mScaleFactor)-bx)-(mPosX/mScaleFactor);
y=((y/mScaleFactor)-by)-(mPosY/mScaleFactor);
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
move=true;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// mPath.rewind();
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
x = ev.getX();
y = ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
// Here update the arraylist in the ArrayList activity
this.arrayClass.incrementArray(mBitmap);
mPath.rewind();
invalidate();
break;}
return true;
}
/*
* more methods here
*/ switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
public void undo() {
// Here recall the last mBitmap from Arraylist activity
this.mBitmap= arrayClass.undo();
mCanvas = new Canvas(mBitmap);
mPath.rewind();
invalidate();
}
}
and this is my ArrayList activity:
public class ArrayClass {
ArrayList<Bitmap> undoArray =new ArrayList<Bitmap>();
private int _size;
private Bitmap _mBitmap;
public void incrementArray(Bitmap mBitmap) {
this._mBitmap=mBitmap;
_size=undoArray.size();
// undoArray.add(_size, _mBitmap);
undoArray.add(_size, Bitmap.createBitmap(_mBitmap));
}
public Bitmap undo() {
// TODO Auto-generated method stub
_size=undoArray.size();
if (_size>1) {
undoArray.remove(_size-1);
_size=undoArray.size();
_mBitmap = ((Bitmap) undoArray.get(_size-1)).copy(Bitmap.Config.ARGB_8888,true);
}
return _mBitmap;
}
public Bitmap redo() {
// TODO
return null;
}
}
Thanks again
Here the solution, edit the ArrayClass in this way:
public class ArrayClass {
ArrayList<Bitmap> undoArray =new ArrayList<Bitmap>();
private int _size;
private Bitmap _mBitmap;
public void incrementArray(Bitmap mBitmap) {
this._mBitmap=mBitmap;
_size=undoArray.size();
// undoArray.add(_size, _mBitmap); replace with:
undoArray.add(_size, Bitmap.createBitmap(_mBitmap));
}
public Bitmap undo() {
// TODO Auto-generated method stub
_ size=undoArray.size();
if (_size>1) {
undoArray.remove(_size-1);
_size=undoArray.size();
_mBitmap = ((Bitmap) undoArray.get(_size-1)).copy(Bitmap.Config.ARGB_8888,true);
}
return _mBitmap;
}
public Bitmap redo() {
// TODO
return null;
}
}

Categories

Resources