先上TV上效果图
Mark下思路:
package com.test.ui; import java.lang.reflect.Method; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Scroller; public class FocusScrollListView extends ListView { //记录当前焦点所在区间 private final byte FOCUS_MIDDLE = 0; private final byte FOCUS_BOTTOM = 1; private final byte FOCUS_TOP = 2; //ListView中每个item的高宽 private int itemWidth; private int itemHeight; //整个ListView的高度 private int listHeight; //焦点所在item的最上面距离parent容器最上面的距离差 private int top; //焦点所在位置 private byte mFocusState = FOCUS_MIDDLE; //背景图片,也可以不用任何图片而直接用一个颜色画一个和item等高等宽的矩形 private Bitmap mBitmap; //是否被调用了setSelection,如果调用了就必须强制刷新焦点图片的位置 private boolean isSetSelection; //Scroller类当前返回的数字,本项目下焦点的Y坐标 private int cordinatesY; //是否是屏幕滑动了,用于按键翻页的,强制刷新焦点图片位置 private boolean isPageScroll; //是否已经拿到了item的高度 private boolean hadHeight; //用于滑动的封装了加速减速器的计数类 private Scroller mScroller; private Matrix m; //scale X和Y,用于将任意图片拉伸到刚好填充item的空间 private float sy; private float sx; //焦点滑动的时间 private int sDuration = 500; //翻页的API private Method method_pageScroll; //item批量上下刷新的API private Method method_arrowScrollImpl; //焦点是否正在滑动的过程中,焦点从一个item滑向相邻的另外一个item且滑动过程结束时该变量即为false private boolean isScroll; //记录离开当前ListView时所在的焦点位置,用于从ListView切换到另外的控件上然后再切换回来还能保持上次焦点所在的位置 private int tmpSelection; private RectF rect; public FocusScrollListView(Context context, AttributeSet attrs) { super(context, attrs); mScroller = new Scroller(context); //禁用ListView上面的渐进边缘,也可以不禁用 setVerticalFadingEdgeEnabled(false); m = new Matrix(); //读取焦点背景图片 mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.btn_focus); //通过反射初始化私有方法 initPrivateMethods(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); itemWidth = getWidth(); listHeight = getHeight(); //ListView刚创建的时候没有child,因为还没有setAdapter,但是创建的时候会调用layout,所以要判断一下,要不然会报错 if (getChildCount() > 0) { //如果获取ListView的高度了就不要再调用这个方法了,要不然某些情况高度会变成0,同时对性能也有好处 if (!hadHeight) { itemHeight = getChildAt(0).getHeight(); hadHeight = true; } sx = (float) itemWidth / mBitmap.getWidth(); sy = (float) itemHeight / mBitmap.getHeight(); m.setScale(sx, sy); //根据item宽高调整好拉伸背景图片 mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, true); } } //所有的效果都是通过这个回调方法完成,这个方法很重要 @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { //如果外面调用了ListView的setSelection方法就会刷新并且返回,不执行下面的代码 if (isSetSelection) { if (null != getSelectedView()) { // canvas.drawBitmap(mBitmap, 0, getSelectedView().getTop(), null); canvas.drawColor(R.color.green); setScroller(getSelectedView().getTop()); isSetSelection = false; return; } } //焦点一边滑动一边刷新,知道Scroller滑动结束,将isScroll置false if (mScroller.computeScrollOffset()) { //不断的回调onDraw invalidate(); } else { if (isScroll) { isScroll = false; } } //如果是翻页就刷新 if (isPageScroll) { if (null != getSelectedView()) { cordinatesY = getSelectedView().getTop(); setScroller(getSelectedView().getTop()); isPageScroll = false; } } else { cordinatesY = mScroller.getCurrY(); } Log.i("ListView", String.valueOf("cordinatesY " + cordinatesY )); //上面一切都是为这个做准备 // canvas.drawBitmap(mBitmap, 0, cordinatesY, null); // 定义画笔1 Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); // 消除锯齿 paint.setAntiAlias(true); // 设置画笔的颜色 paint.setColor(Color.GREEN); // 设置paint的外框宽度 paint.setStrokeWidth(6); // // 画一个圆 // canvas.drawCircle(40, 30, 20, paint); // 画一个正放形 if (cordinatesY > 533) { rect = new RectF(0, 70 + 520, 570, 120 + 520); } else { rect = new RectF(0, 70 + cordinatesY, 570, 120 + cordinatesY); } canvas.drawRoundRect(rect, 10, 10, paint);//第二个参数是x半径,第三个参数是y半径 // canvas.drawRect(0, 70 + cordinatesY, 570, 120 + cordinatesY, paint); // canvas.drawColor(R.color.green); } @Override protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); //离开ListView时候记录焦点位置 if (getChildCount() > 0) { if (!gainFocus) { tmpSelection = getSelectedItemPosition(); } else { if (null != getSelectedView()) { setSelectionFromTop(tmpSelection, getSelectedView() .getTop()); } } } } @Override public void setSelection(int position) { super.setSelection(position); updateFocus(); } /** * The method of setMSelection() instead of setSelection(), so please call * setMSelection to set position of item * * @param position */ public void setMSelection(int position) { setTmpSelection(position); setSelection(position); isPageScroll = true; } private void updateFocus() { isSetSelection = true; } private void setTmpSelection(int position) { tmpSelection = position; } /** * return the number of items at present * * @return the number of items at present */ public int getItemNum() { return getChildCount(); } private void setScroller(int newY) { mScroller.setFinalY(newY); } @Override public void setAdapter(ListAdapter adapter) { super.setAdapter(adapter); setMSelection(0); } //ListView的item数量实际上是动态改变的,会在一个数值x和x+1甚至x+2之间徘徊,所以利用item的数量来计算焦点的移动是不行的,所以增加的实现此功能的复杂度 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //获得当前选中的item View view = getSelectedView(); //下面的就是逻辑上的东西了,在最上,最下,和中间进行不同的移动 if (null != view) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_DOWN: if (getLastVisiblePosition() == getAdapter().getCount() - 1 && getSelectedItemPosition() == getLastVisiblePosition() - 1 && mFocusState == FOCUS_MIDDLE) { top = view.getTop() + itemHeight + getDividerHeight(); mScroller.startScroll(0, view.getTop(), 0, top - view.getTop(), sDuration); isScroll = true; mFocusState = FOCUS_MIDDLE; break; } if (getSelectedItemPosition() < getLastVisiblePosition() - 1) { top = view.getTop() + itemHeight + getDividerHeight(); mScroller.startScroll(0, view.getTop(), 0, top - view.getTop(), sDuration); isScroll = true; mFocusState = FOCUS_MIDDLE; } else if (getSelectedItemPosition() == getLastVisiblePosition() - 1) { if (mFocusState != FOCUS_BOTTOM) { top = listHeight - itemHeight - getVerticalFadingEdgeLength() - getDividerHeight(); mScroller.startScroll(0, view.getTop(), 0, top - view.getTop(), sDuration); mFocusState = FOCUS_BOTTOM; } } break; case KeyEvent.KEYCODE_DPAD_UP: if (getSelectedItemPosition() == getFirstVisiblePosition() + 1) { if (mFocusState != FOCUS_TOP) { top = 0 + getDividerHeight() + getVerticalFadingEdgeLength(); mScroller.startScroll(0, view.getTop(), 0, top - view.getTop(), sDuration); mFocusState = FOCUS_TOP; } break; } if (getSelectedItemPosition() > getFirstVisiblePosition()) { top = view.getTop() - itemHeight - getDividerHeight(); mScroller.startScroll(0, view.getTop(), 0, top - view.getTop(), sDuration); mFocusState = FOCUS_MIDDLE; } break; } } Log.i("ListView", String.valueOf("listHeight " + listHeight + " itemHeight " + itemHeight + " top " + top)); return super.onKeyDown(keyCode, event); } //初始化私有方法 private void initPrivateMethods() { try { method_pageScroll = ListView.class.getDeclaredMethod("pageScroll", int.class); method_arrowScrollImpl = ListView.class.getDeclaredMethod( "arrowScrollImpl", int.class); method_pageScroll.setAccessible(true); method_arrowScrollImpl.setAccessible(true); } catch (Exception e) { e.printStackTrace(); } } //通过此方法设置焦点背景图片 public void setFocusBitmap(int resourceId) { mBitmap = BitmapFactory.decodeResource(getResources(), resourceId); } }
TestActivity:
package com.test.ui; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; public class FocusScrollListViewActivity extends Activity { private FocusScrollListView lv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (FocusScrollListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter(this, R.layout.item, mStrings)); lv.setSelection(0); } private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese", "Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon", "Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase", "Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese", "Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase", "Bishop Kennedy", "Blarney", "Bleu d'Auvergne", "Bleu de Gex", "Bleu de Laqueuille", "Bleu de Septmoncel", "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore", "Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)", "Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon", "Boule Du Roves", "Boulette d'Avesnes", "Boursault", "Boursin", "Bouyssou", "Bra", "Braudostur", "Breakfast Cheese", "Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon", "Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun", "Brillat-Savarin", "Brin", "Brin d' Amour", "Brin d'Amour", "Brinza (Burduf Brinza)", "Briquette de Brebis", "Briquette du Forez", "Broccio", "Broccio Demi-Affine", "Brousse du Rove", "Bruder Basil", "Brusselae Kaas (Fromage de Bruxelles)", "Bryndza", "Buchette d'Anjou", "Buffalo", "Burgos", "Butte", "Butterkase", "Button (Innes)", "Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille", "Caciocavallo", "Caciotta", "Caerphilly", "Cairnsmore", "Calenzana", "Cambazola", "Camembert de Normandie", "Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux", "Capricorn Goat", "Capriole Banon", "Carre de l'Est", "Casciotta di Urbino", "Cashel Blue", "Castellano", "Castelleno", "Castelmagno", "Castelo Branco", "Castigliano", "Cathelain", "Celtic Promise", "Cendre d'Olivet", "Cerney", "Chabichou", "Chabichou du Poitou", "Chabis de Gatine", "Chaource", "Charolais", "Chaumes", "Cheddar", "Cheddar Clothbound", "Cheshire", "Chevres", "Chevrotin des Aravis", "Chontaleno", "Civray", "Coeur de Camembert au Calvados", "Coeur de Chevre", "Colby", "Cold Pack", "Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper", "Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)", "Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese", "Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza", "Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley", "Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino", "Cypress Grove Chevre", "Danablu (Danish Blue)", "Danbo", "Danish Fontina", "Daralagjazsky", "Dauphin", "Delice des Fiouves", "Denhany Dorset Drum", "Derby", "Dessertnyj Belyj", "Devon Blue", "Devon Garland", "Dolcelatte", "Doolin", "Doppelrhamstufel", "Dorset Blue Vinney", "Double Gloucester", "Double Worcester", "Dreux a la Feuille", "Dry Jack", "Duddleswell", "Dunbarra", "Dunlop", "Dunsyre Blue", "Duroblando", "Durrus", "Dutch Mimolette (Commissiekaas)", "Edam", "Edelpilz", "Emental Grand Cru", "Emlett", "Emmental", "Epoisses de Bourgogne", "Esbareich", "Esrom", "Etorki", "Evansdale Farmhouse Brie", "Evora De L'Alentejo", "Exmoor Blue", "Explorateur", "Feta", "Feta (Australian)", "Figue", "Filetta", "Fin-de-Siecle", "Finlandia Swiss", "Finn", "Fiore Sardo", "Fleur du Maquis", "Flor de Guia", "Flower Marie", "Folded", "Folded cheese with mint", "Fondant de Brebis", "Fontainebleau", "Fontal", "Fontina Val d'Aosta", "Formaggio di capra", "Fougerus", "Four Herb Gouda", "Fourme d' Ambert", "Fourme de Haute Loire", "Fourme de Montbrison", "Fresh Jack", "Fresh Mozzarella", "Fresh Ricotta", "Fresh Truffles", "Fribourgeois", "Friesekaas", "Friesian", "Friesla", "Frinault", "Fromage a Raclette", "Fromage Corse", "Fromage de Montagne de Savoie", "Fromage Frais", "Fruit Cream Cheese", "Frying Cheese", "Fynbo", "Gabriel", "Galette du Paludier", "Galette Lyonnaise", "Galloway Goat's Milk Gems", "Gammelost", "Gaperon a l'Ail", "Garrotxa", "Gastanberra", "Geitost", "Gippsland Blue", "Gjetost", "Gloucester", "Golden Cross", "Gorgonzola", "Gornyaltajski", "Gospel Green", "Gouda", "Goutu", "Gowrie", "Grabetto", "Graddost", "Grafton Village Cheddar", "Grana", "Grana Padano", "Grand Vatel", "Grataron d' Areches", "Gratte-Paille", "Graviera", "Greuilh", "Greve", "Gris de Lille", "Gruyere", "Gubbeen", "Guerbigny", "Halloumi", "Halloumy (Australian)", "Haloumi-Style Cheese", "Harbourne Blue", "Havarti", "Heidi Gruyere", "Hereford Hop", "Herrgardsost", "Herriot Farmhouse", "Herve", "Hipi Iti", "Hubbardston Blue Cow", "Hushallsost", "Iberico", "Idaho Goatster", "Idiazabal", "Il Boschetto al Tartufo", "Ile d'Yeu", "Isle of Mull", "Jarlsberg", "Jermi Tortes", "Jibneh Arabieh", "Jindi Brie", "Jubilee Blue", "Juustoleipa", "Kadchgall", "Kaseri", "Kashta", "Kefalotyri", "Kenafa", "Kernhem", "Kervella Affine", "Kikorangi", "King Island Cape Wickham Brie", "King River Gold", "Klosterkaese", "Knockalara", "Kugelkase", "L'Aveyronnais", "L'Ecir de l'Aubrac", "La Taupiniere", "La Vache Qui Rit", "Laguiole", "Lairobell", "Lajta", "Lanark Blue", "Lancashire", "Langres", "Lappi", "Laruns", "Lavistown", "Le Brin", "Le Fium Orbo", "Le Lacandou", "Le Roule", "Leafield", "Lebbene", "Leerdammer", "Leicester", "Leyden", "Limburger", "Lincolnshire Poacher", "Lingot Saint Bousquet d'Orb", "Liptauer", "Little Rydings", "Livarot", "Llanboidy", "Llanglofan Farmhouse", "Loch Arthur Farmhouse", "Loddiswell Avondale", "Longhorn", "Lou Palou", "Lou Pevre", "Lyonnais", "Maasdam", "Macconais", "Mahoe Aged Gouda", "Mahon", "Malvern", "Mamirolle", "Manchego", "Manouri", "Manur", "Marble Cheddar", "Marbled Cheeses", "Maredsous", "Margotin", "Maribo", "Maroilles", "Mascares", "Mascarpone", "Mascarpone (Australian)", "Mascarpone Torta", "Matocq", "Maytag Blue", "Meira", "Menallack Farmhouse", "Menonita", "Meredith Blue", "Mesost", "Metton (Cancoillotte)", "Meyer Vintage Gouda", "Mihalic Peynir", "Milleens", "Mimolette", "Mine-Gabhar", "Mini Baby Bells", "Mixte", "Molbo", "Monastery Cheeses", "Mondseer", "Mont D'or Lyonnais", "Montasio", "Monterey Jack", "Monterey Jack Dry", "Morbier", "Morbier Cru de Montagne", "Mothais a la Feuille", "Mozzarella", "Mozzarella (Australian)", "Mozzarella di Bufala", "Mozzarella Fresh, in water", "Mozzarella Rolls", "Munster", "Murol", "Mycella", "Myzithra", "Naboulsi", "Nantais", "Neufchatel", "Neufchatel (Australian)", "Niolo", "Nokkelost", "Northumberland", "Oaxaca", "Olde York", "Olivet au Foin", "Olivet Bleu", "Olivet Cendre", "Orkney Extra Mature Cheddar", "Orla", "Oschtjepka", "Ossau Fermier", "Ossau-Iraty", "Oszczypek", "Oxford Blue", "P'tit Berrichon", "Palet de Babligny", "Paneer", "Panela", "Pannerone", "Pant ys Gawn", "Parmesan (Parmigiano)", "Parmigiano Reggiano", "Pas de l'Escalette", "Passendale", "Pasteurized Processed", "Pate de Fromage", "Patefine Fort", "Pave d'Affinois", "Pave d'Auge", "Pave de Chirac", "Pave du Berry", "Pecorino", "Pecorino in Walnut Leaves", "Pecorino Romano", "Peekskill Pyramid", "Pelardon des Cevennes", "Pelardon des Corbieres", "Penamellera", "Penbryn", "Pencarreg", "Perail de Brebis", "Petit Morin", "Petit Pardou", "Petit-Suisse", "Picodon de Chevre", "Picos de Europa", "Piora", "Pithtviers au Foin", "Plateau de Herve", "Plymouth Cheese", "Podhalanski", "Poivre d'Ane", "Polkolbin", "Pont l'Eveque", "Port Nicholson", "Port-Salut", "Postel", "Pouligny-Saint-Pierre", "Pourly", "Prastost", "Pressato", "Prince-Jean", "Processed Cheddar", "Provolone", "Provolone (Australian)", "Pyengana Cheddar", "Pyramide", "Quark", "Quark (Australian)", "Quartirolo Lombardo", "Quatre-Vents", "Quercy Petit", "Queso Blanco", "Queso Blanco con Frutas --Pina y Mango", "Queso de Murcia", "Queso del Montsec", "Queso del Tietar", "Queso Fresco", "Queso Fresco (Adobera)", "Queso Iberico", "Queso Jalapeno", "Queso Majorero", "Queso Media Luna", "Queso Para Frier", "Queso Quesadilla", "Rabacal", "Raclette", "Ragusano", "Raschera", "Reblochon", "Red Leicester", "Regal de la Dombes", "Reggianito", "Remedou", "Requeson", "Richelieu", "Ricotta", "Ricotta (Australian)", "Ricotta Salata", "Ridder", "Rigotte", "Rocamadour", "Rollot", "Romano", "Romans Part Dieu", "Roncal", "Roquefort", "Roule", "Rouleau De Beaulieu", "Royalp Tilsit", "Rubens", "Rustinu", "Saaland Pfarr", "Saanenkaese", "Saga", "Sage Derby", "Sainte Maure", "Saint-Marcellin", "Saint-Nectaire", "Saint-Paulin", "Salers", "Samso", "San Simon", "Sancerre", "Sap Sago", "Sardo", "Sardo Egyptian", "Sbrinz", "Scamorza", "Schabzieger", "Schloss", "Selles sur Cher", "Selva", "Serat", "Seriously Strong Cheddar", "Serra da Estrela", "Sharpam", "Shelburne Cheddar", "Shropshire Blue", "Siraz", "Sirene", "Smoked Gouda", "Somerset Brie", "Sonoma Jack", "Sottocenare al Tartufo", "Soumaintrain", "Sourire Lozerien", "Spenwood", "Sraffordshire Organic", "St. Agur Blue Cheese", "Stilton", "Stinking Bishop", "String", "Sussex Slipcote", "Sveciaost", "Swaledale", "Sweet Style Swiss", "Swiss", "Syrian (Armenian String)", "Tala", "Taleggio", "Tamie", "Tasmania Highland Chevre Log", "Taupiniere", "Teifi", "Telemea", "Testouri", "Tete de Moine", "Tetilla", "Texas Goat Cheese", "Tibet", "Tillamook Cheddar", "Tilsit", "Timboon Brie", "Toma", "Tomme Brulee", "Tomme d'Abondance", "Tomme de Chevre", "Tomme de Romans", "Tomme de Savoie", "Tomme des Chouans", "Tommes", "Torta del Casar", "Toscanello", "Touree de L'Aubier", "Tourmalet", "Trappe (Veritable)", "Trois Cornes De Vendee", "Tronchon", "Trou du Cru", "Truffe", "Tupi", "Turunmaa", "Tymsboro", "Tyn Grug", "Tyning", "Ubriaco", "Ulloa", "Vacherin-Fribourgeois", "Valencay", "Vasterbottenost", "Venaco", "Vendomois", "Vieux Corse", "Vignotte", "Vulscombe", "Waimata Farmhouse Blue", "Washed Rind Cheese (Australian)", "Waterloo", "Weichkaese", "Wellington", "Wensleydale", "White Stilton", "Whitestone Farmhouse", "Wigmore", "Woodside Cabecou", "Xanadu", "Xynotyro", "Yarg Cornish", "Yarra Valley Pyramid", "Yorkshire Blue", "Zamorano", "Zanetti Grana Padano", "Zanetti Parmigiano Reggiano"}; }