Showing posts with label circular gridview in android. Show all posts
Sunday, December 29, 2013
CircularImageView Using Custom View
Manchu Bhargavi
10:55 AM
android
,
circular gridview in android
,
circularimageview in android
,
customview
,
customview extends view class in android example
,
customview using circular image in android
,
imageview using gridview
23 comments
:
Hi,
This is an Example for CustomView extends View Class.
Introduction:
Own view typically classified as custom views and compound views.
compound views are constructor based on existing views with some predefined layout and logic
and it extends on View group class.
custom view extends view class and draw on themselves
Introduction:
Own view typically classified as custom views and compound views.
compound views are constructor based on existing views with some predefined layout and logic
and it extends on View group class.
custom view extends view class and draw on themselves
Creating custom view:
By extending viewclass or one of its subclass you can create your own view.It is responsible for measuring,layouting,drawing themselves and their child elements and also saving UI state and handling touch events.
Reason for creating view:
View are typically created to avoid repetition code
By extending viewclass or one of its subclass you can create your own view.It is responsible for measuring,layouting,drawing themselves and their child elements and also saving UI state and handling touch events.
Reason for creating view:
View are typically created to avoid repetition code
CutomView.class:
package com.example.circularImageview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CustomView extends ImageView
{
private int color;
private Paint paint;
private Rect rect;
private RectF rectF;
private Bitmap output;
private Canvas canvas;
private float roundPx;
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(),getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
}
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
// TODO Auto-generated method stub
int targetWidth = 1000;
int targetHeight =630;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight) / 2,(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CCW);
//path.addCircle(50,50,50,Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
}
MainActivity.java:
package com.example.circularImageview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private LruCache<String, Bitmap> mMemoryCache;
GridView gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gd=(GridView)findViewById(R.id.gridView1);
gd.setAdapter(new MyAdapter(this));
gd.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(),"your position is "+arg2, 4).show();
}
});
}
public class MyAdapter extends BaseAdapter
{
private List<Item> items=new ArrayList<Item>();
private LayoutInflater inflator;
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
inflator=LayoutInflater.from(context);
items.add(new Item("nature1",R.drawable.img1));
items.add(new Item("nature2",R.drawable.img2));
items.add(new Item("nature3",R.drawable.img3));
items.add(new Item("nature4",R.drawable.img4));
items.add(new Item("nature5", R.drawable.img5));
items.add(new Item("nature6",R.drawable.img2));
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return items.get(position).drawableId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View v=convertView;
ImageView img1;
TextView txt1;
if(v==null)
{
v=inflator.inflate(R.layout.grid_item,parent,false);
v.setTag(R.id.picture,v.findViewById(R.id.picture));
v.setTag(R.id.text,findViewById(R.id.text));
}
img1=(ImageView)v.findViewById(R.id.picture);
txt1=(TextView)v.findViewById(R.id.text);
Item item=(Item)getItem(position);
// final String imageKey = String.valueOf(item.drawableId);
CustomView cv=new CustomView(MainActivity.this);
//final Bitmap bitmap = mMemoryCache.get(imageKey);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), item.drawableId);
img1.setImageBitmap(cv.getRoundedShape(bmp));
// img1.setImageResource(item.drawableId);
txt1.setText(item.name);
return v;
}
}
private class Item
{
final String name;
final int drawableId;
Item(String name,int drawableId)
{
this.name=name;
this.drawableId=drawableId;
}
}
}
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
>
</GridView>
</FrameLayout>
list.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView>
</RelativeLayout>
Here the Output is shown below
Subscribe to:
Posts
(
Atom
)
