Skip to content

Instantly share code, notes, and snippets.

@ethankhall
Last active November 8, 2021 02:17
Show Gist options
  • Save ethankhall/5518832 to your computer and use it in GitHub Desktop.
Save ethankhall/5518832 to your computer and use it in GitHub Desktop.
Half Circle List View
import android.widget.ListView;
import android.widget.AbsListView;
import android.content.Context;
public class HalfCircleListView extends ListView implements AbsListView.OnScrollListener {
public HalfCircleListView(Context context) {
super(context);
setOnScrollListener(this);
}
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
//Ignored
}
@Override
public void onScroll(AbsListView absListView, int i, int i2, int i3) {
//Part of the magic happens here
absListView.invalidateViews();
}
}
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ListView listView = new HalfCircleListView(this);
List<String> listOfStrings = new ArrayList<String>();
for(int i = 0; i < 50 ; i++){
listOfStrings.add("Value " + i);
}
listView.setAdapter(new ScrollAdapter(listOfStrings));
setContentView(listView);
}
}
import android.widget.TextView;
import android.content.Context;
import android.graphics.Canvas;
import android.util.DisplayMetrics;
import android.content.res.Resources;
public class MyView extends TextView {
private static final int MAX_INDENT = 300;
private static final String TAG = MyView.class.getSimpleName();
public MyView(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
canvas.save();
float indent = getIndent(getY());
//Part of the magic happens here too
canvas.translate(indent, 0);
super.onDraw(canvas);
canvas.restore();
}
public float getIndent(float distance){
float x_vertex = MAX_INDENT;
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
float y_vertex = displayMetrics.heightPixels / 2 / displayMetrics.density;
double a = ( 0 - x_vertex ) / ( Math.pow(( 0 - y_vertex), 2) ) ;
float indent = (float) (a * Math.pow((distance - y_vertex), 2) + x_vertex);
return indent;
}
}
import java.util.List;
import android.widget.BaseAdapter;
import android.view.ViewGroup;
import android.view.View;
import android.util.Log;
public class ScrollAdapter extends BaseAdapter {
private List<String> scrollViews;
private static final String TAG = ScrollAdapter.class.getSimpleName();
public ScrollAdapter(List<String> scrollViews){
this.scrollViews = scrollViews;
}
@Override
public int getCount() {
return scrollViews.size();
}
@Override
public String getItem(int i) {
return scrollViews.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = new MyView(viewGroup.getContext());
}
MyView currentView = (MyView) view;
String itemViewType = getItem(i);
Log.d(TAG, itemViewType);
((MyView) view).setText(itemViewType);
return currentView;
}
}
@sumeetguha
Copy link

Hello,

I was trying to make this circular list horizontally, instead of vertically, but not getting the expected result.
Could you please tell me, what exact changes are required to make the horizontal circular list.

Thanks,
Sumeet Guha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment