I am trying to make listview [slidable from left] of navigation drawer layout fullscreen that is make its width to fullscreen.But i am not able to do it.any suggestions?
I want to remove the right gap
Sent from my GT-S5570 using XDA Premium 4 mobile app
Yes, you have to extend DrawerLayout and override some methods because MIN_DRAWER_MARGIN is private
Code:
public class FullDrawerLayout extends DrawerLayout {
private static final int MIN_DRAWER_MARGIN = 0; // dp
public FullDrawerLayout(Context context) {
super(context);
}
public FullDrawerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
throw new IllegalArgumentException(
"DrawerLayout must be measured with MeasureSpec.EXACTLY.");
}
setMeasuredDimension(widthSize, heightSize);
// Gravity value for each drawer we've seen. Only one of each permitted.
int foundDrawers = 0;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (isContentView(child)) {
// Content views get measured at exactly the layout's size.
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
child.measure(contentWidthSpec, contentHeightSpec);
} else if (isDrawerView(child)) {
final int childGravity =
getDrawerViewGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
if ((foundDrawers & childGravity) != 0) {
throw new IllegalStateException("Child drawer has absolute gravity " +
gravityToString(childGravity) + " but this already has a " +
"drawer view along that edge");
}
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
MIN_DRAWER_MARGIN + lp.leftMargin + lp.rightMargin,
lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
lp.topMargin + lp.bottomMargin,
lp.height);
child.measure(drawerWidthSpec, drawerHeightSpec);
} else {
throw new IllegalStateException("Child " + child + " at index " + i +
" does not have a valid layout_gravity - must be Gravity.LEFT, " +
"Gravity.RIGHT or Gravity.NO_GRAVITY");
}
}
}
boolean isContentView(View child) {
return ((LayoutParams) child.getLayoutParams()).gravity == Gravity.NO_GRAVITY;
}
boolean isDrawerView(View child) {
final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
final int absGravity = Gravity.getAbsoluteGravity(gravity,
child.getLayoutDirection());
return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
}
int getDrawerViewGravity(View drawerView) {
final int gravity = ((LayoutParams) drawerView.getLayoutParams()).gravity;
return Gravity.getAbsoluteGravity(gravity, drawerView.getLayoutDirection());
}
static String gravityToString(int gravity) {
if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
return "LEFT";
}
if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
return "RIGHT";
}
return Integer.toHexString(gravity);
}
}
Can you elaborate more.how to use this class
Sent from my GT-S5570 using XDA Premium 4 mobile app
Related
I've been struggling for a couple of weeks to solve this problem. I have two expandable views, one inside another. Whenever i click on the second level expandable view items, it won't inflate its children. All i see when i click is a small scroll bar that appears on the right side on the expandable view item itself. Using a simple layout with a fixed textview results in the same behaviour, meaning that the problem isn't within the ShiftListItem object/s.
Also, getChildView() is never been called.
YearListAdapter:
Code:
public class YearListAdapter extends BaseExpandableListAdapter {
private Context _context;
private SparseArray<SparseArray<ArrayList<Salary>>> _shiftYears;
public YearListAdapter(
SparseArray<SparseArray<ArrayList<Salary>>> shiftYears,
Context context) {
super();
_context = context;
_shiftYears = shiftYears;
}
public Object getChild(int arg0, int arg1) {
return (null == _shiftYears.get(_shiftYears.keyAt(arg0))) ? null
: _shiftYears.get(_shiftYears.keyAt(arg0)).get(
_shiftYears.get(_shiftYears.keyAt(arg0)).keyAt(arg1));
}
public long getChildId(int arg0, int arg1) {
return arg1;
}
public View getChildView(int arg0, int arg1, boolean arg2,
View convertView, ViewGroup arg4) {
MonthsListView monthLevelExpandable = new MonthsListView(_context);
MonthListAdapter adapter = new MonthListAdapter(_context,
_shiftYears.get(_shiftYears.keyAt(arg0)));
monthLevelExpandable.setAdapter(adapter);
adapter.forceReload();
monthLevelExpandable.setGroupIndicator(null);
return monthLevelExpandable;
}
public int getChildrenCount(int arg0) {
if (null == _shiftYears.get(_shiftYears.keyAt(arg0)))
return 0;
return _shiftYears.get(_shiftYears.keyAt(arg0)).size();
}
public Object getGroup(int arg0) {
return (null == _shiftYears) ? null : _shiftYears.get(_shiftYears
.keyAt(arg0));
}
public int getGroupCount() {
return _shiftYears.size();
}
public long getGroupId(int arg0) {
return arg0;
}
public View getGroupView(int arg0, boolean isExpanded, View convertView,
ViewGroup arg3) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.year_list_view_item, null);
}
TextView title = (TextView) convertView.findViewById(R.id.yearTitle);
title.setText("" + _shiftYears.keyAt(arg0));
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
public void forceReload() {
notifyDataSetChanged();
}
}
MonthListAdapter:
Code:
public class MonthListAdapter extends BaseExpandableListAdapter {
private Context _context;
private SparseArray<ArrayList<Salary>> _shiftMonths;
public MonthListAdapter(Context context,
SparseArray<ArrayList<Salary>> shiftMonths) {
super();
_context = context;
_shiftMonths = shiftMonths;
}
public Object getChild(int groupPosition, int childPosition) {
return (null == _shiftMonths) ? null : _shiftMonths.get(
_shiftMonths.keyAt(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ShiftListItem sli;
if (null == convertView) {
sli = (ShiftListItem) View.inflate(_context,
R.layout.shift_list_item, null);
} else {
sli = (ShiftListItem) convertView;
}
sli.setSalary(_shiftMonths.get(_shiftMonths.keyAt(groupPosition)).get(
childPosition));
return sli;
}
public int getChildrenCount(int groupPosition) {
if (null == _shiftMonths
|| null == _shiftMonths.get(_shiftMonths.keyAt(groupPosition)))
return 0;
return _shiftMonths.get(_shiftMonths.keyAt(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
return (null == _shiftMonths) ? null : _shiftMonths.get(_shiftMonths
.keyAt(groupPosition));
}
public int getGroupCount() {
return _shiftMonths.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.month_list_view_item, null);
}
TextView title = (TextView) convertView.findViewById(R.id.monthTitle);
title.setText("" + _shiftMonths.keyAt(groupPosition));
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void forceReload() {
notifyDataSetChanged();
}
}
ShiftListItem:
Code:
public class ShiftListItem extends LinearLayout {
private Salary salary;
private TextView itemTimeAndDate;
private TextView itemCashView;
public ShiftListItem(Context context, AttributeSet attrs) {
super(context, attrs);
salary = new Salary();
}
[user=439709]@override[/user]
protected void onFinishInflate() {
super.onFinishInflate();
itemTimeAndDate = (TextView) findViewById(R.id.itemTimeAndDate);
itemCashView = (TextView) findViewById(R.id.itemCashView);
}
public void setSalary(Salary salary) {
this.salary = salary;
Calendar start = salary.getShiftTime().getStart();
Calendar end = salary.getShiftTime().getEnd();
int tHour;
int tMinute;
int tSec;
tSec = salary.getShiftTime().getDifference(
salary.getShiftTime().getStart(),
salary.getShiftTime().getEnd());
tHour = tSec / 3600;
tSec -= tHour * 3600;
tMinute = tSec / 60;
tSec -= tMinute * 60;
itemTimeAndDate.setText("Time: (Start) "
+ returnTwoDigitString(start.get(Calendar.HOUR_OF_DAY)) + ":"
+ returnTwoDigitString(start.get(Calendar.MINUTE)) + " (End)"
+ returnTwoDigitString(end.get(Calendar.HOUR_OF_DAY)) + ":"
+ returnTwoDigitString(end.get(Calendar.MINUTE)) + "\n"
+ (start.get(Calendar.DAY_OF_MONTH)) + "-"
+ (end.get(Calendar.DAY_OF_MONTH)) + "/"
+ (start.get(Calendar.MONTH) + 1) + "-"
+ (end.get(Calendar.MONTH) + 1) + "/"
+ start.get(Calendar.YEAR) + "-" + end.get(Calendar.YEAR));
itemCashView.setText(salary.getCash() + " " + salary.getCurrency()
+ " Total Time Of: " + returnTwoDigitString(tHour) + ":"
+ returnTwoDigitString(tMinute) + ":"
+ returnTwoDigitString(tSec));
}
public Salary getSalary() {
return salary;
}
private String returnTwoDigitString(int time) {
if (time < 10) {
return "0" + time;
}
return "" + time;
}
}
shift_list_item.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<com...ShiftListItem xmlns:android="....."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/itemTimeAndDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false" />
<TextView
android:id="@+id/itemCashView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false" />
</com.....ShiftListItem>
I am currently working on an App Switcher with the ability to also add custom apps in the app switcher. So, I already got the recent apps loader built. This is the code for this part of the app:
Code:
public class Corners_RecentApps extends Activity {
private ArrayList<PanelItemDetail> rowItems = null;
private ListView listView;
private ArrayList<String> packageName = null;
private ArrayList<String> className = null;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean rightpanel = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("panelpos_right", true);
if(rightpanel){
overridePendingTransition(R.anim.left_slide_in_fast, 0);
setContentView(R.layout.right_side_panel);
}
else
{
overridePendingTransition(R.anim.right_slide_in_fast, 0);
setContentView(R.layout.activity_left_side_panel);
}
ImageView imgbtn = (ImageView) findViewById(R.id.transparentbackground);
ImageView panelbg = (ImageView) findViewById(R.id.panelbackground);
listView = (ListView)findViewById(R.id.panelcontents);
packageName = new ArrayList<String>();
className = new ArrayList<String>();
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> tasks = am.getRecentTasks(30, 0);
rowItems = new ArrayList<PanelItemDetail>();
PackageManager pacMgr = getPackageManager();
for (ActivityManager.RecentTaskInfo recentTask : tasks) {
try {
rowItems.add(new PanelItemDetail(pacMgr.getApplicationIcon(recentTask.origActivity.getPackageName())));
packageName.add(recentTask.origActivity.getPackageName());
className.add(recentTask.origActivity.getClassName());
Log.d("#@#", "getPackageName = " + recentTask.origActivity.getPackageName());
Log.d("#@#", "getClassName = " + recentTask.origActivity.getClassName());
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
String itembg = myPreference.getString("itembg_list", "");
if(itembg.equals("defaults"))
{
PanelArrayAdapter adapter = new PanelArrayAdapter(this,R.layout.panelrow_default, rowItems);
listView.setAdapter(adapter);
}
else if(itembg.equals("dark"))
{
PanelArrayAdapter adapter = new PanelArrayAdapter(this,R.layout.panelrow_dark, rowItems);
listView.setAdapter(adapter);
}
else if(itembg.equals("light"))
{
PanelArrayAdapter adapter = new PanelArrayAdapter(this,R.layout.panelrow_light, rowItems);
listView.setAdapter(adapter);
}
else
{
PanelArrayAdapter adapter = new PanelArrayAdapter(this,R.layout.panelrow_none, rowItems);
listView.setAdapter(adapter);
}
listView.setOnItemClickListener(new OnItemClickListener() {
[user=439709]@override[/user]
public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {
try{
boolean rightpanel = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE).getBoolean("panelpos_right", true);
Intent taskintent = getPackageManager().getLaunchIntentForPackage(packageName.get(postion).toString());
startActivity(taskintent);
if(rightpanel){
overridePendingTransition(R.anim.right_slide_in, R.anim.zoom_out);
}
else
{
overridePendingTransition(R.anim.left_slide_in, R.anim.zoom_out);
}
finish();
}
catch (NullPointerException fail) {
Toast.makeText(getApplicationContext(), "!", Toast.LENGTH_SHORT).show();
}
}
});
SharedPreferences panelbgpref = PreferenceManager.getDefaultSharedPreferences(this);
String panelbgset = panelbgpref.getString("panelbg_list", "");
if(panelbgset.equals("light"))
{
panelbg.setImageResource(R.drawable.panelbg_light);
}
else
{
panelbg.setImageResource(R.drawable.panelbg);
}
imgbtn.setOnClickListener(new View.OnClickListener(){
[user=439709]@override[/user]
public void onClick(View v) {
if(v.getId() ==R.id.transparentbackground){
moveTaskToBack(true);
finish();
}
}
});
}
Now I want to let the users define in the app settings up to 3 own apps that should be shown on every moment.
How should I do that?
Thank you
Hi!
I'm developing an App that register the data speed sended and received. The problem is that the app shows a very similar speed between upload and upload, and the data speed seems to be incorrect.
Could anyone solve my problem?
Sorry form my BAD English [emoji29]
This is my code:
Code:
public class Traffic extends TextView {
private int mDelaytime = 3000;
private Handler mHandler = new Handler();
private int mLastReceive = 0;
private int mLastTransmit = 0;
private int mTxRate = 0;
private int mRxRate = 0;
File traffic = new File("/data/.traffic");
private Runnable task = new Runnable() {
public void run() {
if (!traffic.exists() || mLastReceive == getTotalDataBytes(true) && mLastTransmit == getTotalDataBytes(false)){
mTxRate = (getTotalDataBytes(false) - mLastTransmit);
mRxRate = (getTotalDataBytes(true) - mLastReceive);
mLastReceive = getTotalDataBytes(true);
mLastTransmit = getTotalDataBytes(false);
Traffic.this.setText(formatSize(mTxRate/3) + "\n" + formatSize(mRxRate/3));
} else {
Traffic.this.setText("");
}
mHandler.postDelayed(task, mDelaytime);
}
};
private int getTotalDataBytes(boolean Transmit) {
String readLine;
String[] DataPart;
int line = 0;
int Data = 0;
try {
FileReader fr = new FileReader("/proc/net/dev");
BufferedReader br = new BufferedReader(fr);
while((readLine = br.readLine()) != null) {
line++;
if (line <= 2) continue;
DataPart = readLine.split(":");
DataPart = DataPart[1].split("\\s+");
if (Transmit) {
Data += Integer.parseInt(DataPart[1]);
} else {
Data += Integer.parseInt(DataPart[9]);
}
}
fr.close();
br.close();
} catch (IOException e) {
return -1;
}
return Data;
}
public Traffic(Context context) {
this(context, null);
}
public Traffic(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Traffic(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mHandler.postDelayed(task, mDelaytime);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeCallbacks(task);
}
private static final String BYTES = "B/s";
private static final String MEGABYTES = "MB/s";
private static final String KILOBYTES = "KB/s";
private static final String GIGABYTES = "GB/s";
private static final long KILO = 1024;
private static final long MEGA = KILO * 1024;
private static final long GIGA = MEGA * 1024;
static String formatSize(final long pBytes) {
if (pBytes < KILO) {
return pBytes + BYTES;
} else if (pBytes < MEGA) {
return (int) (0.5 + (pBytes / (double) KILO)) + KILOBYTES;
} else if (pBytes < GIGA) {
return (int) (0.5 + (pBytes / (double) MEGA)) + MEGABYTES;
} else {
return (int) (0.5 + (pBytes / (double) GIGA)) + GIGABYTES;
}
}
}
Thanks!!
Come on please! :'(
Enviado desde mi GT-S7580 mediante Tapatalk
DannyGM16 said:
Hi!
I'm developing an App that register the data speed sended and received. The problem is that the app shows a very similar speed between upload and upload, and the data speed seems to be incorrect.
Could anyone solve my problem?
Sorry form my BAD English [emoji29]
This is my code:
Code:
public class Traffic extends TextView {
private int mDelaytime = 3000;
private Handler mHandler = new Handler();
private int mLastReceive = 0;
private int mLastTransmit = 0;
private int mTxRate = 0;
private int mRxRate = 0;
File traffic = new File("/data/.traffic");
private Runnable task = new Runnable() {
public void run() {
if (!traffic.exists() || mLastReceive == getTotalDataBytes(true) && mLastTransmit == getTotalDataBytes(false)){
mTxRate = (getTotalDataBytes(false) - mLastTransmit);
mRxRate = (getTotalDataBytes(true) - mLastReceive);
mLastReceive = getTotalDataBytes(true);
mLastTransmit = getTotalDataBytes(false);
Traffic.this.setText(formatSize(mTxRate/3) + "\n" + formatSize(mRxRate/3));
} else {
Traffic.this.setText("");
}
mHandler.postDelayed(task, mDelaytime);
}
};
private int getTotalDataBytes(boolean Transmit) {
String readLine;
String[] DataPart;
int line = 0;
int Data = 0;
try {
FileReader fr = new FileReader("/proc/net/dev");
BufferedReader br = new BufferedReader(fr);
while((readLine = br.readLine()) != null) {
line++;
if (line <= 2) continue;
DataPart = readLine.split(":");
DataPart = DataPart[1].split("\\s+");
if (Transmit) {
Data += Integer.parseInt(DataPart[1]);
} else {
Data += Integer.parseInt(DataPart[9]);
}
}
fr.close();
br.close();
} catch (IOException e) {
return -1;
}
return Data;
}
public Traffic(Context context) {
this(context, null);
}
public Traffic(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Traffic(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mHandler.postDelayed(task, mDelaytime);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeCallbacks(task);
}
private static final String BYTES = "B/s";
private static final String MEGABYTES = "MB/s";
private static final String KILOBYTES = "KB/s";
private static final String GIGABYTES = "GB/s";
private static final long KILO = 1024;
private static final long MEGA = KILO * 1024;
private static final long GIGA = MEGA * 1024;
static String formatSize(final long pBytes) {
if (pBytes < KILO) {
return pBytes + BYTES;
} else if (pBytes < MEGA) {
return (int) (0.5 + (pBytes / (double) KILO)) + KILOBYTES;
} else if (pBytes < GIGA) {
return (int) (0.5 + (pBytes / (double) MEGA)) + MEGABYTES;
} else {
return (int) (0.5 + (pBytes / (double) GIGA)) + GIGABYTES;
}
}
}
Thanks!!
Click to expand...
Click to collapse
Maybe this help: https://github.com/NetEase/Emmagee
I don't understand Chinese xD
And it doesn't help me... but anyway Thanks!
Any other solution?
Enviado desde mi GT-S7580 mediante Tapatalk
Oh didnt see that, but there are other data traffic apps on github just search with google. Sorry thats all i can do
Hi,
I'm trying to apply a Mask or Blur Filter to some buttons at runtime... This button is reflecting a "layer" object, so I'd like to blur its edges to reflect how transparent is this layer...
I'm able to set a ColorFilter, or apply a maskFIlter to its TextView, but cant figure how to apply a maskFilter to the whole button...
here is the part of my code where I'd like to apply this filter (in a applyFilter method):
Code:
public void createList(LinearLayout lm) {
lm.removeAllViews();
for (int i = 0; i < kwang07Android.nblayers; i++) {
final Button newbtn = new Button(this, null, android.R.attr.buttonStyleSmall);
int col = kwang07Android.layers[i].col;
int newColor = Color.argb(156+(int)(kwang07Android.layers[i].opacite*100), Color.red(col), Color.green(col), Color.blue(col));
newbtn.getBackground().setColorFilter(newColor, MULTIPLY);
[COLOR="DarkOrange"]//applyFilter(newbtn, i, BlurMaskFilter.Blur.INNER);[/COLOR]
newbtn.setText("couche " + Integer.toString(i));
newbtn.setId(i);
newbtn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,30+kwang07Android.layers[i].thickness/20));
final int finalI = i;
newbtn.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
showColorPickerDialogDemo(newbtn, finalI);
}
return false;
}
});
lm.addView(newbtn);
}
...
Any advice?
Thanks.
As what was stated on the header I want to implement either a "paint" function for user to edit paint/censor unwanted parts of a photo displayed on a imageview before uploading it to a server in the edited format and a redo function if user makes a mistake while editing?
How do I come about doing it, I've read relevant topics on Canvas, or FingerPaint but still puzzled on how to implement it based on my project here? Tried referencing to the links here and here but without success in implementing the codes into my project code due to my lack of programming skills.
Thanks for any help rendered!
Tried integrating the codes below into my code above (image preview after taking a photo with the camera) for user to start editing via painting but still not working? Thanks for any help rendered!
Code:
public class Drawing extends View {
private Paint mPaint, mBitmapPaint;
Intent intent = getIntent();
Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
private Canvas mCanvas;
private Path mPath;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private int color, size, state;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private ArrayList<Integer> colors = new ArrayList<Integer>();
private ArrayList<Integer> sizes = new ArrayList<Integer>();
public Drawing(Context c) {
super(c);
}
public Drawing(Context c,int width, int height, int size, int color, int state) {
super(c);
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
// mBitmapPaint = new Paint(Paint.DITHER_FLAG);
// mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
setColor(color);
setSize(size);
setState(state);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
// canvas.drawColor(Color.TRANSPARENT);
// canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
//
// if (state == 0)
// mBitmap.eraseColor(Color.TRANSPARENT);
for (int i = 0; i < paths.size(); i++) {
mPaint.setColor(colors.get(i));
mPaint.setStrokeWidth(sizes.get(i));
canvas.drawPath(paths.get(i), mPaint);
}
mPaint.setColor(color);
mPaint.setStrokeWidth(size);
canvas.drawPath(mPath, mPaint);
}
public void setColor(int color) {
this.color = color;
}
public void setSize(int size) {
this.size = size;
}
public void setState(int state) {
this.state = state;
// if (state == 0)
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// else
// mPaint.setXfermode(null);
}
public void onClickUndo() {
if (paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
sizes.remove(sizes.size() - 1);
colors.remove(colors.size() - 1);
invalidate();
}
}
private void touch_start(float x, float y) {
undonePaths.clear();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
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;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
colors.add(color);
sizes.add(size);
paths.add(mPath);
mPath = new Path();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.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();
invalidate();
break;
}
return true;
}
}