編輯:關於Android編程
public class MyViewFlipper extends RelativeLayout implements OnTouchListener,OnGestureListener{ private ViewFlipper viewflipper; private LogView logView; private Context parentContext; TextView textView; TextView[] textViews; RFIDDAQ rfiddaq; final static int viewflipperid=100; GestureDetector detector;//創建手勢監聽的對象 /** * 再次匹配的時間間隔 單位 秒 */ private int time; private String address; private int maxshownum;//顯示的最大數量,超過這個數量會刷新掉之前的數據。 public MyViewFlipper(Context context) { super(context); this.parentContext=context; detector=new GestureDetector(this); init(); } private void init() { // TODO Auto-generated method stub // 創建滾動顯示框對象 viewflipper = new ViewFlipper(parentContext); viewflipper.setBackgroundColor(Color.CYAN); viewflipper.setId(viewflipperid); LayoutParams lpviewflipper = new LayoutParams(-1, 500); // 創建信息采集框對象,放入viewflipper中作為元素之一 //這裡的rfiddaq以及下面的都可以logView都可以替換成自定義的View視圖。當然也可以添加新的自定義試圖,只需要添加到viewflipper中就可以了。 rfiddaq = new RFIDDAQ(); lpviewflipper.addRule(RelativeLayout.ALIGN_PARENT_TOP); viewflipper.addView(rfiddaq); // 創建logView顯示界面,放入viewflipper中作為元素之一 logView = new LogView(parentContext); viewflipper.addView(logView); // 這裡添加的LinearLayout是包含小圓點的集合 LinearLayout viewGroup = new LinearLayout(parentContext); ViewGroup group = (ViewGroup) viewGroup; textViews = new TextView[viewflipper.getChildCount()]; for (int i = 0; i < viewflipper.getChildCount(); i++) { textView = new TextView(parentContext); textView.setLayoutParams(new LayoutParams(30, 30)); textView.setPadding(0, 0, 2, 0); textViews[i] = textView; if (i == 0) { // 默認進入程序後第一張圖片被選中,因為viewflipper默認就是選中第一個視圖 textViews[i].setBackgroundResource(R.drawable.radio_sel); } else { textViews[i].setBackgroundResource(R.drawable.radio); } group.addView(textViews[i]); } // 設置小圓圈的位置 LayoutParams lpgroup = new LayoutParams(-2, -2); lpgroup.addRule(RelativeLayout.BELOW, viewflipperid); lpgroup.addRule(RelativeLayout.CENTER_HORIZONTAL); // 把viewflipper和顯示小圓圈框的viewGroup放入RelativeLayout中 addView(viewflipper, lpviewflipper); addView(viewGroup, lpgroup); viewflipper.setOnTouchListener(this);//添加動作監聽 viewflipper.setLongClickable(true);//這一句必須添加,否則手勢監聽無法觸發 } public RFIDDAQ getRfiddaq() { return rfiddaq; } public void setRfiddaq(RFIDDAQ rfiddaq) { this.rfiddaq = rfiddaq; } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub if(e2.getX()-e1.getX()>0){<div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="comment">//如果滑動到最後 則停止切換 </span><span> </span></span></li><li class=""><span> <span class="keyword">if</span><span>(viewflipper.getDisplayedChild() == </span><span class="number">0</span><span>){ </span></span></li><li class="alt"><span> viewflipper.stopFlipping(); </span></li><li class=""><span> }<span class="keyword">else</span><span>{ </span></span></li><li class="alt"><span> <span class="comment">/**</span> </span></li><li class=""><span><span class="comment"> * 這裡其實可以添加一些動畫效果。</span> </span></li><li class="alt"><span><span class="comment"> * 如:viewflipper.setInAnimation(parentContext,R.anim.push_right_in); </span> </span></li><li class=""><span><span class="comment"> */</span><span> </span></span></li><li class="alt"><span> viewflipper.showNext(); </span></li><li class=""><span> showRound(viewflipper.getDisplayedChild()); </span></li><li class="alt"><span> } </span></li><li class=""><span> }<span class="keyword">else</span><span> </span><span class="keyword">if</span><span>(e2.getX()-e1.getX()<</span><span class="number">0</span><span>){ </span></span></li><li class="alt"><span> <span class="comment">//如果滑動到最後 則停止切換 </span><span> </span></span></li><li class=""><span> <span class="keyword">if</span><span>(viewflipper.getDisplayedChild() == </span><span class="number">1</span><span>){ </span></span></li><li class="alt"><span> viewflipper.stopFlipping(); </span></li><li class=""><span> }<span class="keyword">else</span><span>{ </span></span></li><li class="alt"><span> viewflipper.showNext(); </span></li><li class=""><span> showRound(viewflipper.getDisplayedChild()); </span></li><li class="alt"><span> } </span></li><li class=""><span> }<span class="keyword">else</span><span>{ </span></span></li><li class="alt"><span> </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Override</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> onLongPress(MotionEvent e) { </span></span></li><li class="alt"><span> <span class="comment">// TODO Auto-generated method stub</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Override</span><span> </span></span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">boolean</span><span> onScroll(MotionEvent e1, MotionEvent e2, </span><span class="keyword">float</span><span> distanceX, </span></span></li><li class="alt"><span> <span class="keyword">float</span><span> distanceY) { </span></span></li><li class=""><span> <span class="comment">// TODO Auto-generated method stub</span><span> </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Override</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> onShowPress(MotionEvent e) { </span></span></li><li class=""><span> <span class="comment">// TODO Auto-generated method stub</span><span> </span></span></li><li class="alt"><span> </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Override</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">boolean</span><span> onSingleTapUp(MotionEvent e) { </span></span></li><li class=""><span> <span class="comment">// TODO Auto-generated method stub</span><span> </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Override</span><span> </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">boolean</span><span> onTouch(View v, MotionEvent event) { </span></span></li><li class=""><span> <span class="comment">// TODO Auto-generated method stub</span><span> </span></span></li><li class="alt"><span> detector.onTouchEvent(event); </span></li><li class=""><span> <span class="keyword">return</span><span> </span><span class="keyword">false</span><span>; </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> showRound(</span><span class="keyword">int</span><span> position){ </span></span></li><li class=""><span> <span class="keyword">for</span><span>(</span><span class="keyword">int</span><span> i=</span><span class="number">0</span><span>;i<textViews.length;i++){ </span></span></li><li class="alt"><span> <span class="keyword">if</span><span>(i==position){ </span></span></li><li class=""><span> textViews[i].setBackgroundResource(R.drawable.radio_sel); </span></li><li class="alt"><span> }<span class="keyword">else</span><span>{ </span></span></li><li class=""><span> textViews[i].setBackgroundResource(R.drawable.radio); </span></li><li class="alt"><span> } </span></li><li class=""><span> } </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span>} </span></li></ol></div><pre name="code" class="java" style="display: none;">//如果滑動到最後 則停止切換 if(viewflipper.getDisplayedChild() == 0){ viewflipper.stopFlipping(); }else{ /** * 這裡其實可以添加一些動畫效果。 * 如:viewflipper.setInAnimation(parentContext,R.anim.push_right_in); */ viewflipper.showNext(); showRound(viewflipper.getDisplayedChild()); } }else if(e2.getX()-e1.getX()<0){ //如果滑動到最後 則停止切換 if(viewflipper.getDisplayedChild() == 1){ viewflipper.stopFlipping(); }else{ viewflipper.showNext(); showRound(viewflipper.getDisplayedChild()); } }else{ } return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub detector.onTouchEvent(event); return false; } public void showRound(int position){ for(int i=0;i<textViews.length;i++){ if(i==position){ textViews[i].setBackgroundResource(R.drawable.radio_sel); }else{ textViews[i].setBackgroundResource(R.drawable.radio); } } } }</pre> <pre></pre>
1. 樣式資源解析(1) 樣式簡介樣式解析: 樣式是設置給 View 組件的多個屬性的集合;--樣式的好處: 給一個 TextView 設置 文字大小, 顏色, 對齊方式
好長時間沒更博客了,最近一直在做比賽的一個項目,就是實現客戶端和PC端的文件互傳,其實一開始在看到這個題目的時候,完全不知道怎麼去實現,感覺一臉懵逼,後來在查閱了資料以及
最近在項目中需要對外部存儲設備的狀態進行監聽,所以整理了此筆記,以便日後查看。 外部存儲設備的狀態變化時發出的廣播
public class DragGrid extends GridView { /** 點擊時候的X位置 */ public int downX; /