Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to fix my code but I keep failing :(

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to fix my code but I keep failing :(

    I've got this code that makes the image turn into a circle using a ListView. But as seen in the image, it dies when it goes to the end of the photo (the last few pixels are carried on when there not supposed to).

    I don't want the photo to take up the whole page, just the size of the photo. Here's the code and XML:

    mIdOK.jpg

    Stream item xml:

     
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
     
    </RelativeLayout>
    activity stream xml:
     
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_list"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:divider="@null"
        android:dividerHeight="0px"
        android:drawSelectorOnTop="true"
        tools:context=".StreamActivity" />
    main code:
    public class StreamActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
            setContentView(R.layout.activity_stream);
     
            StreamAdapter adapter = new StreamAdapter(this);
            ((ListView) findViewById(R.id.main_list)).setAdapter(adapter);
     
            adapter.add(new StreamItem(this, R.drawable.testimage1, null, null));
            adapter.add(new StreamItem(this, R.drawable.testimage1, null, null));
            adapter.add(new StreamItem(this, R.drawable.testimage1, null, null));
            adapter.add(new StreamItem(this, R.drawable.testimage1, null, null));
     
        }
     
        class StreamItem {
            final Bitmap mBitmap;
     
     
            StreamItem(Context c, int resid, String line1, String line2) {
                mBitmap = BitmapFactory.decodeResource(c.getResources(), resid);
     
            }
        }
     
        class StreamDrawable extends Drawable {
            private static final boolean USE_VIGNETTE = true;
     
            private final float mCornerRadius;
            private final RectF mRect = new RectF();
            private final BitmapShader mBitmapShader;
            private final Paint mPaint;
            private final int mMargin;
     
            StreamDrawable(Bitmap bitmap, float cornerRadius, int margin) {
                mCornerRadius = cornerRadius;
     
                mBitmapShader = new BitmapShader(bitmap,
                        Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
     
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setShader(mBitmapShader);
     
                mMargin = margin;
            }
     
            @Override
            protected void onBoundsChange(Rect bounds) {
                super.onBoundsChange(bounds);
                mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin);
     
                if (USE_VIGNETTE) {
                    RadialGradient vignette = new RadialGradient(
                            mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f,
                            new int[] { 0, 0, 0x7f000000 }, new float[] { 0.0f, 0.7f, 1.0f },
                            Shader.TileMode.CLAMP);
     
                    Matrix oval = new Matrix();
                    oval.setScale(1.0f, 0.7f);
                    vignette.setLocalMatrix(oval);
     
                    mPaint.setShader(
                            new ComposeShader(mBitmapShader, vignette, PorterDuff.Mode.SRC_OVER));
                }
            }
     
            @Override
            public void draw(Canvas canvas) {
                canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
            }
     
            @Override
            public int getOpacity() {
                return PixelFormat.TRANSLUCENT;
            }
     
            @Override
            public void setAlpha(int alpha) {
                mPaint.setAlpha(alpha);
            }
     
            @Override
            public void setColorFilter(ColorFilter cf) {
                mPaint.setColorFilter(cf);
            }       
        }
     
     
        class StreamAdapter extends ArrayAdapter<StreamItem> {
            private static final int CORNER_RADIUS =100; // dips
            private static final int MARGIN = 1; // dips
     
            private final int mCornerRadius;
            private final int mMargin;
            private final LayoutInflater mInflater;
     
            public StreamAdapter(Context context) {
                super(context, 0);
     
                final float density = context.getResources().getDisplayMetrics().density;
                mCornerRadius = (int) (CORNER_RADIUS * density + 0.5f);
                mMargin = (int) (MARGIN * density + 0.5f);
     
                mInflater = LayoutInflater.from(getContext());
            }
     
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewGroup view = null;
     
                if (convertView == null) {
                    view = (ViewGroup) mInflater.inflate(R.layout.stream_item, parent, false);
                } else {
                    view = (ViewGroup) convertView;
                }
     
                StreamItem item = getItem(position);
     
                StreamDrawable d = new StreamDrawable(item.mBitmap, mCornerRadius, mMargin);
                view.setBackgroundDrawable(d);
     
                int w = item.mBitmap.getWidth();
                int h = item.mBitmap.getHeight();
     
                float ratio = w / (float) h;
     
                LayoutParams lp = view.getLayoutParams();
                lp.width = getContext().getResources().getDisplayMetrics().widthPixels;
                lp.height = (int) (lp.width / ratio);
     
                return view;
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to fix my code but I keep failing :(

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to fix my code but I keep failing :(

    Any help is appreciated
    Last edited by james2013; February 12th, 2013 at 08:26 AM. Reason: fixing stuff

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Trying to fix my code but I keep failing :(

    I think Actually you want reduce the size of the image in listview?
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  5. #5

    Default Re: Trying to fix my code but I keep failing :(

    It may be possible that after decreasing size of image you will get done. After reading your post, i got this solution in my mind.

Similar Threads

  1. Please anyone can you fix my Code??
    By java_rookie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 09:59 AM
  2. How do i fix my code?
    By beebee007 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2012, 10:49 AM
  3. What do i have to do to fix my code?
    By kram in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 07:13 AM
  4. How to fix this code?
    By ice in forum AWT / Java Swing
    Replies: 26
    Last Post: November 29th, 2010, 07:10 PM