Skip to content

Commit 9c15d33

Browse files
Cache of SlideHolder is now cleared on each update. On new versions (VERSION >= HONEYCOMB) cache is updated only when main layout is marked as dirty. Added getMenuOffset() method.
1 parent d7526ef commit 9c15d33

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

example/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@
129129

130130
<RelativeLayout
131131
android:layout_width="fill_parent"
132-
android:layout_height="fill_parent"
133-
android:background="@android:color/white" >
132+
android:layout_height="fill_parent" >
134133

135134
<TextView
136135
android:id="@+id/textView"
@@ -140,6 +139,7 @@
140139
android:layout_centerVertical="true"
141140
android:text="@string/swipe"
142141
android:textSize="25sp" />
142+
143143
</RelativeLayout>
144144

145145
</com.agimind.widget.SlideHolder>

example/src/com/agimind/widget/SlideHolder.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import android.content.Context;
2323
import android.graphics.Bitmap;
2424
import android.graphics.Canvas;
25+
import android.graphics.Color;
2526
import android.graphics.Paint;
27+
import android.graphics.PorterDuff.Mode;
2628
import android.graphics.Rect;
2729
import android.graphics.Region.Op;
30+
import android.os.Build;
2831
import android.util.AttributeSet;
2932
import android.view.MotionEvent;
3033
import android.view.View;
@@ -143,6 +146,10 @@ public void setAlwaysOpened(boolean opened) {
143146
requestLayout();
144147
}
145148

149+
public int getMenuOffset() {
150+
return mOffset;
151+
}
152+
146153
public void setOnSlideListener(OnSlideListener lis) {
147154
mListener = lis;
148155
}
@@ -363,8 +370,25 @@ protected void onMeasure(int wSp, int hSp) {
363370
protected void dispatchDraw(Canvas canvas) {
364371
try {
365372
if(mMode == MODE_SLIDE) {
366-
if(++mFrame % 5 == 0) { //redraw every 5th frame
367-
getChildAt(1).draw(mCachedCanvas);
373+
View main = getChildAt(1);
374+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
375+
/*
376+
* On new versions we redrawing main layout only
377+
* if it's marked as dirty
378+
*/
379+
if(main.isDirty()) {
380+
mCachedCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
381+
main.draw(mCachedCanvas);
382+
}
383+
} else {
384+
/*
385+
* On older versions we just redrawing our cache
386+
* every 5th frame
387+
*/
388+
if(++mFrame % 5 == 0) {
389+
mCachedCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
390+
main.draw(mCachedCanvas);
391+
}
368392
}
369393

370394
/*
@@ -427,6 +451,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
427451
MotionEvent cancelEvent = MotionEvent.obtain(ev);
428452
cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
429453
super.dispatchTouchEvent(cancelEvent);
454+
cancelEvent.recycle();
430455
}
431456

432457
return true;
@@ -539,6 +564,8 @@ private void initSlideMode() {
539564
if(mCachedBitmap == null || mCachedBitmap.isRecycled() || mCachedBitmap.getWidth() != v.getWidth()) {
540565
mCachedBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
541566
mCachedCanvas = new Canvas(mCachedBitmap);
567+
} else {
568+
mCachedCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
542569
}
543570

544571
v.setVisibility(View.VISIBLE);

sidemenu.jar

267 Bytes
Binary file not shown.

src/com/agimind/widget/SlideHolder.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import android.content.Context;
2323
import android.graphics.Bitmap;
2424
import android.graphics.Canvas;
25+
import android.graphics.Color;
2526
import android.graphics.Paint;
27+
import android.graphics.PorterDuff.Mode;
2628
import android.graphics.Rect;
2729
import android.graphics.Region.Op;
30+
import android.os.Build;
2831
import android.util.AttributeSet;
2932
import android.view.MotionEvent;
3033
import android.view.View;
@@ -143,6 +146,10 @@ public void setAlwaysOpened(boolean opened) {
143146
requestLayout();
144147
}
145148

149+
public int getMenuOffset() {
150+
return mOffset;
151+
}
152+
146153
public void setOnSlideListener(OnSlideListener lis) {
147154
mListener = lis;
148155
}
@@ -363,8 +370,25 @@ protected void onMeasure(int wSp, int hSp) {
363370
protected void dispatchDraw(Canvas canvas) {
364371
try {
365372
if(mMode == MODE_SLIDE) {
366-
if(++mFrame % 5 == 0) { //redraw every 5th frame
367-
getChildAt(1).draw(mCachedCanvas);
373+
View main = getChildAt(1);
374+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
375+
/*
376+
* On new versions we redrawing main layout only
377+
* if it's marked as dirty
378+
*/
379+
if(main.isDirty()) {
380+
mCachedCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
381+
main.draw(mCachedCanvas);
382+
}
383+
} else {
384+
/*
385+
* On older versions we just redrawing our cache
386+
* every 5th frame
387+
*/
388+
if(++mFrame % 5 == 0) {
389+
mCachedCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
390+
main.draw(mCachedCanvas);
391+
}
368392
}
369393

370394
/*
@@ -427,6 +451,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
427451
MotionEvent cancelEvent = MotionEvent.obtain(ev);
428452
cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
429453
super.dispatchTouchEvent(cancelEvent);
454+
cancelEvent.recycle();
430455
}
431456

432457
return true;
@@ -539,6 +564,8 @@ private void initSlideMode() {
539564
if(mCachedBitmap == null || mCachedBitmap.isRecycled() || mCachedBitmap.getWidth() != v.getWidth()) {
540565
mCachedBitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
541566
mCachedCanvas = new Canvas(mCachedBitmap);
567+
} else {
568+
mCachedCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
542569
}
543570

544571
v.setVisibility(View.VISIBLE);

0 commit comments

Comments
 (0)