Drawing image in each item of a a listView in a linear layout?
So, I'm trying to draw a Circle on Touch for each item of a ListView.
This is my ListItem:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linkCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/linkTitle0001"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="4dp"
android:lineSpacingExtra="-2dp"
android:fontFamily="sans-serif-condensed"
android:maxLines="3"
android:text="hihi"
android:textColor="#717171"
android:textSize="49sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="imageOnClick"
android:src="@drawable/ic_launcher"/>
<com.example.piemenu.PieMenu
android:id="@+id/circle_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
</FrameLayout>
This is where I fit in the ListItem in a ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</LinearLayout>
This is my adapter:
public class ListArrayAdapter extends ArrayAdapter<String> {
public ArrayList<String> links = new ArrayList<String>();
public Activity activity = new Activity();
TextView linkText;
TextView linkDesc;
public Context c;
public ListArrayAdapter(Context context, int textViewResourceId,
ArrayList<String> fiokiList, Object o) {
super(context, textViewResourceId, fiokiList);
this.links = fiokiList;
activity = (Activity) (o);
c = context;
//notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
final String link = this.links.get(position);
linkText = (TextView) convertView.findViewById(R.id.linkTitle0001);
// linkImageView = (SmartImageView)
convertView.findViewById(R.id.faviconView0001);
linkText.setText(link);
FrameLayout fl =
(FrameLayout)convertView.findViewById(R.id.linkCardView);
ImageView imgView =
(ImageView)convertView.findViewById(R.id.imageView1);
PieMenu pieMenu = new PieMenu(imgView, activity);
fl.addView(pieMenu);
return convertView;
}
}
And this is my PieMenu class:
public class PieMenu extends FrameLayout{
// Linear
private static int ANIMATOR_DEC_SPEED15 = 1;
private static int ANIMATOR_ACC_SPEED15 = 2;
// Cascade
private static int ANIMATOR_ACC_INC_1 = ANIMATOR_ACC_SPEED15 + 1;
private static int ANIMATOR_ACC_INC_15 = ANIMATOR_ACC_INC_1 + 15;
// Special purpose
private static int ANIMATOR_BATTERY_METER = ANIMATOR_ACC_INC_15 + 1;
private static int ANIMATOR_SNAP_GROW = ANIMATOR_ACC_INC_15 + 2;
private static int ANIMATOR_END = ANIMATOR_SNAP_GROW;
private Paint mPieBackground = new Paint(COLOR_PIE_BACKGROUND);
private Paint mPieSelected = new Paint(COLOR_PIE_SELECT);
private Paint mPieOutlines = new Paint(COLOR_PIE_OUTLINES);
private Paint mSnapBackground = new Paint(COLOR_SNAP_BACKGROUND);
private static final int COLOR_OUTLINES_MASK = 0x22000000;
private static final int COLOR_ALPHA_MASK = 0xaa000000;
private static final int COLOR_OPAQUE_MASK = 0xff000000;
private static final int COLOR_SNAP_BACKGROUND = 0xffffffff;
private static final int COLOR_PIE_BACKGROUND = 0xaa000000;
private static final int COLOR_PIE_BUTTON = 0xb2ffffff;
private static final int COLOR_PIE_SELECT = 0xaaffffff;
private static final int COLOR_PIE_OUTLINES = 0x55ffffff;
private static final int COLOR_CHEVRON_LEFT = 0x0999cc;
private static final int COLOR_CHEVRON_RIGHT = 0x53d5e5;
private static final int COLOR_BATTERY_JUICE = 0x33b5e5;
private static final int COLOR_BATTERY_JUICE_LOW = 0xffbb33;
private static final int COLOR_BATTERY_JUICE_CRITICAL = 0xff4444;
private static final int COLOR_BATTERY_BACKGROUND = 0xffffff;
private static final int COLOR_STATUS = 0xffffff;
private static final int BASE_SPEED = 1000;
private static final int CHEVRON_FRAGMENTS = 16;
private static final float SIZE_BASE = 1.0f;
private ImageView _view;
private ArrayList<PieItem> mItems;
private Context _context;
private Animation animFadeOut;
public PieMenu(ImageView view, Context context) {
super(context);
_context = context;
_view = view;
setWillNotDraw(false);
mItems = new ArrayList<PieItem>();
// TODO Auto-generated constructor stub
}
public PieMenu(Context context, AttributeSet attrs) {
super(context, attrs);
// ~
}
public PieMenu(View view, Context context, AttributeSet attrs) {
super(context, attrs);
// ~
}
private CustomValueAnimator[] mAnimators = new
CustomValueAnimator[ANIMATOR_END + 1];
public void addPieMenu(int x, int y){
mItems = new ArrayList<PieItem>();
Path path = new Path();
path.addCircle(9, 9, 9, Path.Direction.CW);
PieItem item = new PieItem(_view,_context,path, x,y,40);
mItems.add(item);
//FrameLayout.LayoutParams lyp = new
FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
//pieView.setLayoutParams(lyp);
//addView(pieView);
// animateIn();
}
private class CustomValueAnimator {
@SuppressLint("NewApi")
public CustomValueAnimator(int animateIndex) {
index = animateIndex;
manual = false;
animateIn = true;
animator = ValueAnimator.ofInt(0, 1);
animator.addUpdateListener(new
CustomAnimatorUpdateListener(index));
fraction = 0;
}
@SuppressLint("NewApi")
public void start() {
if (!manual) {
animator.setDuration(duration);
animator.start();
}
}
@SuppressLint("NewApi")
public void reverse(int milliSeconds) {
if (!manual) {
animator.setDuration(milliSeconds);
animator.reverse();
}
}
@SuppressLint("NewApi")
public void cancel() {
animator.cancel();
fraction = 0;
}
public int index;
public int duration;
public boolean manual;
public boolean animateIn;
public float fraction;
public ValueAnimator animator;
}
private class CustomAnimatorUpdateListener implements
ValueAnimator.AnimatorUpdateListener {
private int mIndex;
CustomAnimatorUpdateListener(int index) {
mIndex = index;
}
@SuppressLint("NewApi")
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mAnimators[mIndex].fraction = animation.getAnimatedFraction();
invalidate();
}
}
private void animateIn() {
// Cancel & start all animations
cancelAnimation();
invalidate();
for (int i = 0; i < mAnimators.length; i++) {
//mAnimators[i].animateIn = true;
mAnimators[i].start();
}
}
private void cancelAnimation() {
for (int i = 0; i < mAnimators.length; i++) {
if (mAnimators[1] != null)
mAnimators[i].cancel();
}
}
@SuppressLint({ "NewApi", "DrawAllocation" })
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
// Toast.makeText(_context, "text",Toast.LENGTH_LONG).show();
/*canvas.drawCircle ('1', '2', (true ?
mAnimators[ANIMATOR_SNAP_GROW].fraction *
Math.max(getWidth(), getHeight()) * 1.5f : 0),
mSnapBackground);
mSnapBackground.setAlpha((int)(10.0));
int len = (int)(10.0 * 1.3f);
int thick = (int)(len * 0.2f);
Path plus = new Path();
plus.addRect(10 - len / 2, 10 - thick / 2, 10 + len / 2, 10 +
thick / 2, Path.Direction.CW);
plus.addRect(10 - thick / 2, 10 - len / 2, 10 + thick / 2, 10 +
len / 2, Path.Direction.CW);
canvas.drawPath(plus, mSnapBackground);
canvas.save();
*/
if(mItems != null)
{
for (int i = 0; i < mItems.size();i++)
{
Paint paint1 = new Paint();
Bitmap bitmap = Bitmap.createBitmap(10, 10,
Config.ARGB_8888);
//canvas.drawBitmap(bitmap,
mItems.get(i).x+1,mItems.get(i).y+8, paint1);
Bitmap bgr =
BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Paint transparentpainthack = new Paint();
transparentpainthack.setAlpha(100);
// canvas.drawBitmap(bgr, mItems.get(i).x,
mItems.get(i).y, transparentpainthack);
ValueAnimator animator;
animator = ValueAnimator.ofFloat(0, 1); // values
from 0 to 1
animator.setDuration(5000); // 5 seconds duration
from 0 to 1
paint1.setColor(Color.RED);
paint1.setAntiAlias(true);
paint1.setFilterBitmap(true);
paint1.setDither(true);
paint1.setStyle(Paint.Style.STROKE);
//Bitmap myBitmap =
BitmapFactory.decodeResource(getResources(),
BitmapDrawable drawable = (BitmapDrawable)
_view.getDrawable();
Bitmap bitmap1 = drawable.getBitmap();
canvas.drawBitmap(bitmap1, mItems.get(i).x,
mItems.get(i).y, transparentpainthack);
canvas.drawCircle(mItems.get(i).x, mItems.get(i).y,
mItems.get(i).r + 50, paint1);
animator.addUpdateListener(new
ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator
animation) {
float value = ((Float)
(animation.getAnimatedValue()))
.floatValue();
// Set translation of your view here.
Position can be calculated
// out of value. This code should move the
view in a half circle.
_view.setTranslationX((float)(200.0 *
Math.sin(value*Math.PI)));
_view.setTranslationY((float)(200.0 *
Math.cos(value*Math.PI)));
}
});
}
}
// this.draw(canvas);
//_view.draw(canvas);
//for (PieItem item : mItems) {
// //if (!canItemDisplay(item)) continue;
//drawItem(canvas, item);
//\] }
}
/*
@SuppressLint("NewApi")
private void drawItem(Canvas canvas, PieItem item) {
if (item.getView() != null) {
int state = canvas.save();
float angle = (float) Math.abs(Math.acos(10)) * 2.0f;
canvas.rotate(angle + 20, 10, 10);
canvas.drawPath(item.getPath(), item.isSelected() ?
mPieSelected : mPieBackground);
canvas.drawPath(item.getPath(), mPieOutlines);
canvas.restoreToCount(state);
state = canvas.save();
ImageView view = (ImageView)item.getView();
canvas.translate(view.getX(), view.getY());
// canvas.rotate(angle,(float)10,view.getWidth() / 2,
view.getHeight() / 2);
view.draw(canvas);
canvas.restoreToCount(state);
}
}
*/
@Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getPointerCount() > 1) return true;
final float touchX = evt.getRawX();
final float touchY = evt.getRawY();
BitmapDrawable drawable = (BitmapDrawable) _view.getDrawable();
Bitmap bitmap1 = drawable.getBitmap();
Log.d("height",String.valueOf(bitmap1.getWidth()));
if(mItems.isEmpty()){
Path path = new Path();
path.addCircle(9, 9, 9, Path.Direction.CW);
PieItem item = new PieItem(_view,_context,path,
touchX,touchY,40);
mItems.add(item);
}
switch (evt.getAction()) {
case MotionEvent.ACTION_DOWN:
//if (mItems == null){
//}
for(int i = 0;i<mItems.size();i++){
// Toast.makeText(_context,
"tas",Toast.LENGTH_SHORT).show();
if (touchX >= mItems.get(i).x &&
touchX <= (mItems.get(i).x + bitmap1.getWidth()) &&
touchY >= mItems.get(i).y &&
touchY <= (mItems.get(i).y + bitmap1.getHeight())) {
// click code
Toast.makeText(_context, "asa",
Toast.LENGTH_SHORT).show();
}
}
break;
}
//Toast.makeText(_context, String.valueOf(mX),
Toast.LENGTH_LONG).show();
invalidate();
return true;
}
}
I'm just finding it a bit difficult to understand how to draw a
cirle/piemenu for onTouch of each item of a listview.
No comments:
Post a Comment