ref
http://desert3.iteye.com/blog/1596020
http://stackoverflow.com/questions/6856028/difference-between-processbuilder-and-runtime-exec
廣告
2014年12月28日 星期日
2014年12月4日 星期四
[Use github to show(deploy) your profile html page][使用github發布html網頁]
[作業系統window 7 ]
如果想簡單發佈一個個人簡介的網頁,
可以考慮用github。
step1. 在github local端create project ,命名方式 => your github name.github.io
ex:cihm.github.io
step2. 將你的網頁相關檔案放到你local端 你剛創的 github路徑。
step3. commit and sync
step4, 網址列打上your github name.github.io
ex:http://cihm.github.io/
樣本網路上有很多種,
可以參考
http://html5up.net/
他們都是響應式網頁設計(Responsive web design,通常縮寫為RWD,
也就是可以自適應各種裝置的大小。
如果想簡單發佈一個個人簡介的網頁,
可以考慮用github。
step1. 在github local端create project ,命名方式 => your github name.github.io
ex:cihm.github.io
step2. 將你的網頁相關檔案放到你local端 你剛創的 github路徑。
step3. commit and sync
step4, 網址列打上your github name.github.io
ex:http://cihm.github.io/
樣本網路上有很多種,
可以參考
http://html5up.net/
他們都是響應式網頁設計(Responsive web design,通常縮寫為RWD,
也就是可以自適應各種裝置的大小。
2014年12月1日 星期一
[WebService _ first project_ use eclipse,tomcat][第一個專案(Hello World)使用tomcat and eclipse]
download tomcat7 ,
1.使用者變數JAVA_HOME ===> C:\Program Files (x86)\Java\jdk1.7.0_06
系統變數 path ===>C:%JAVA_HOME%\bin
系統變數 path ===>C:%JAVA_HOME%\bin
2.啟動 tomcat => cmd=> cd C:\Program Files\apache-tomcat-7.0.56\bin , startup
3.在網址列打上 http://127.0.0.1:8080/ (管理介面)
4.設定Tomcat目錄下的conf資料夾內的tomcat-users.xml
<role rolename="manager-gui"/> <user username="lewis" password="lewis" roles="manager-gui"/>
===============================================================================
eclipse基本流程
- Servlet網站的Project類型是Dynamic Web Project。
- 可以在Web.xml裡面設定Servlet對應路徑等,這邊勾選的"Generate web.xml deployment descriptor"指的就是那個xml(也叫做Deployment Descriptor)
- 增加一個Servlet,對新建的project點右鍵選New->Servlet:
- 預設URL Mapping是對應到和這個Servlet名稱一樣(這個例子是HelloWorld),可以修改Pattern
- 設定Target Runtime
如果沒有設定Target Runtime這個時候Servlet沒有辦法Build。因為會需要Servlet相關物件,因此需要加入對應的實作class。使用Tomcat,所以要先把他加入才可以。
先加一個Tomcat的Server。在View 「Servers」裡面選擇新增,並且選Tomcat 7(路徑同上一篇) - 最後需要把那個Server的class加進去。對Project點右鍵「Properties」找到"Java Build Path"選擇"Add Library"選"Server Runtime"
- 增加get顯示頁面
回到「HelloWorld.java」,設定doGet()的內容:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String requestUri = request.getRequestURI();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Hello world</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello World from frisrt servlet");
out.println("<form action='" + requestUri + "' method='post'>");
out.println("<input type='text' name='name' />");
out.println("<input type='submit' value='submit' />");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
增加post顯示頁面
這邊是把輸入的東西在顯示出來:
這邊是把輸入的東西在顯示出來:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Hello world</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello: " + name);
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
把Server Start起來以後,可以再IE直接輸入: http://localhost/ServletSample/TestServlet
===============================================================================
設定Servlet連接
如何把一個Servlet從另一個Servlet區分呢?
- 靠設定檔。
- Servlet 3.0後增加了@WebServlet 標註功能
- java reflection and invoke
1. 靠設定檔
基本上會有一個Web.xml檔案(或者可以用annotation的方式設定),告訴我們的Container(Tomcat)當某一個路徑進來的時候,呼叫哪一個Servlet來處理。
servlet
這裡面的設定比較像servlet的對應。
這個裡面一定會有包含兩個element:
servlet-name:表示代表這個servlet的名字,方便其他部份reference。在整個web.xml是不可以重複的。
servlet-class:代表著這個servlet的Class名稱。這邊需要Fully Qualified Name, 也就是包含package名稱
那servlet下面還可以代入預設參數。這個意思是,我們可以再處理request的時候,透過getInitParameter()的方式讀取一些預設在這邊的參數。
格式是init-para表示一個參數,而其下面則是param-name和param-value代表著key, Value。
servlet設定範例<servlet> <servlet-name>Log4JInitServlet</servlet-name> <servlet-class>com.ws.controller.Log4JInitServlet</servlet-class> <init-param> <param-name>log4j-properties-location</param-name> <param-value>/WEB-INF/properties/log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
servlet-mapping
這個部份是要設定什麼路徑使用什麼Servlet。
有兩個基本element:
servlet-name:那一個servlet要對應到(上面設定servlet-name的對應)
url-pattern:相對路徑下面所要對應。
servlet-mapping設定範例<servlet-mapping> <servlet-name>Log4JInitServlet</servlet-name> <url-pattern>/Log4JInitServlet</url-pattern> </servlet-mapping>表示只要url進來對應為/Log4JInitServlet,就使用在servlet定義的那個servlet-class作為處理的object。
2. Servlet 3.0後增加了@WebServlet 標註功能
Servlet 3.0後增加了@WebServlet 標註功能
只要Servlet上有這行標註,container就會自動讀取資訊
範例中的標註指的是這支Servlet會處理經由 /Log4JInitServlet這個虛擬路徑來的HTTP請求
@WebServlet標籤還能透過參數做更多的設定:
@WebServlet(name="Log4JInitServlet", urlPatterns={"/Log4JInitServlet"}, loadOnStartup=1)
1 and 2 's result
- 所以將標註當成預設值,web.xml進行管理也是可以的
- 設定的套用順序會是 標註 -> web.xml
3. java reflection and invoke
- 之後再補充這部分
===============================================================================
Deploy web service
[1]use tomcat manager to load XXX,war , xxx is your project be exported
[2] 在C:\Program Files (x86)\apache-tomcat-7.0.56\webapps 下創建一個資料夾,
"結構 如附圖" or eclipse
WEB-INF底下必須放置名為 web.xml的部屬描述檔
META-INF下的MANIFEST.MF宣告程式庫的相依性
有需要再設定即可,一般不用管它
ib資料夾底下放置會被引用到web application的JAR檔
web application使用類別時,會先到classes裡尋找是否有該類別檔案
如果沒有就再到container實作中放classes檔的地方或lib目錄底下尋找
[Web Service Introuction ][初探&介紹]
Servlet生命週期簡單的概括分為四步:class加載實例化--->初始化--->服務--->銷毀。
Servlet生命週期是由javax.servlet.Servlet接口定義,所有的Servlet都必須實現這個接口。在Servlet接口中定義了5個方法,其中3個方法代表了Servlet的生命周期:
1. init方法:負責初始化Servlet對象。
2. service方法:負責回應客戶的請求。
3. destroy方法:當Servlet對象退出生命週期時,負責釋放佔用的資源。
1、加載(Load)和實例化(Instantiated)
Servlet容器負責加載和實例化Servlet。因為容器是通過Java的反射API來創建Servlet實例,調用的是Servlet的默認構造方法(即不帶參數的構造方法),所以我們在編寫Servlet類的時候,不應該提供帶參數的構造方法。 Servlet容器加載Servlet,有以下幾種情況:(1)、Servlet容器啟動時自動裝載Servlet,讀取配置文件web.XML文件中的屬性,如果為1,則容器啟動時加載Servlet.(2)、在Servlet容器啟動後,客戶首次向Servlet發送請求。 Servlet容器會判斷內存中是否存在指定的Servlet對象,如果沒有則加載這個Servlet。(3)、Servlet類文件被更新後,重新加載Servlet.
2、初始化階段:init(ServletConfig conf)(補充)
在Servlet實例化之後,Servlet容器將調用Servlet的init()方法初始化這個對象。初始化的目的是為了讓Servlet對像在處理客戶端請求前完成一些初始化的工作,如建立數據庫的連接,獲取配置信息等。對於每一個Servlet實例,init()方法只被調用一次。在初始化期間,Servlet實例可以使用Servlet容器為它準備的ServletConfig對像從Web應用程序的配置信息(在web.xml中配置)中獲取初始化的參數信息。在初始化期間,如果發生錯誤,Servlet實例可以拋出ServletException異常或者UnavailableException異常來通知容器。 ServletException異常用於指明一般的初始化失敗,例如沒有找到初始化參數;而UnavailableException異常用於通知容器該Servlet實例不可用。例如,數據庫服務器沒有啟動,數據庫連接無法建立,Servlet就可以拋出UnavailableException異常向容器指出它暫時或永久不可用。
3、回應請求服務階段(service)
Servlet 被初始化以後,就處於能回應請求的就緒狀態。在service()方法中,Servlet實例通過ServletRequest對象得到客戶端的相關信息和請求信息,在對請求進行處理後,調用ServletResponse對象的方法設置回應訊息。當客戶端有一個請求時,Servlet容器將ServletRequest 和ServletResponse對像都轉發給Servlet,這兩個對像以參數的形式傳給service方法。這個方法由javax.servlet.Servlet定義並由具體的Servlet 實現。注意:客戶端每次請求Servlet都會運行該方法,該方法判斷訪問類型,然後根據HttpServletRequest的getMethod()方法返回結果判斷是執行doGet還是doPost,doPut。而且無論請求多少次Servlet,最多只有一個Servlet實例。多個客戶端並發請求Servlet時,服務器會啟動多個線程分別執行該Servlet的service()方法。在service()方法執行期間,如果發生錯誤,Servlet實例可以拋出ServletException異常或者UnavailableException異常。如果UnavailableException異常指示了該實例永久不可用,Servlet容器將調用實例的destroy()方法,釋放該實例。此後對該實例的任何請求,都將收到容器發送的HTTP 404(請求的資源不可用)回應。如果UnavailableException異常指示了該實例暫時不可用,那麼在暫時不可用的時間段內,對該實例的任何請求,都將收到容器發送的HTTP 503(服務器暫時忙,不能處理請求)回應。
4、終止服務階段(destroy)
當需要釋放內存或者容器關閉時,容器就會調用Servlet實例的destroy()方法。在destroy()方法調用之後,容器會釋放這個Servlet實例,該實例隨後會被Java的垃圾收集器所回收。如果再次需要這個Servlet處理請求,Servlet容器會創建一個新的Servlet實例。
小結:
在整個Servlet的生命週期過程中,創建Servlet實例、調用實例的init()和destroy()方法都只進行一次,當初始化完成後,Servlet容器會將該實例保存在內存中,通過調用它的service()方法,為接收到的請求服務。 Servlet有時會用到一些需要初始化與銷毀的資源,因此可以把初始化資源的代碼放入init()方法內,把銷毀資源的代碼放入destroy方法內,而不需要每次處理請求都要初始化與銷毀資源。
補充=====================================================================
when Servlet init() load
http://guoliangqi.iteye.com/blog/667258=======================================================================
Servlet/JSP處理HTTP請求的過程是:
- 使用者對Web Server發出HTTP請求
- Server收到請求後將其轉給container,並由container分析請求內容後建立相對應的物件
- container根據請求裡的URL分配給正確的Servlet 並為這個請求建立或配制執行緒,以及將請求物件與回應物件交予該執行緒處理
- container呼叫Servlet的sevice() method 根據請求的類別,service()會再呼叫doGet()或doPost() method
- 接著由所呼叫的method建置動態網頁,並將它放入回應物件裡
- 執行緒結束,container轉換回應物件為HTTP回應傳回Client端 接著刪除請求與回應物件
2014年11月30日 星期日
[Android 顯示從網路下載的圖片][Android show picture from url]
常常會用到即時從網路下載圖片,並顯示於原件上。
下載圖片的方式有很多種,這邊用AsyncTask 搭配 http GET的方式顯示
圖片在imageView上,(可以自行將imageView 改成其他元件,grid, list等等)。
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; public class Lewis extends Activity { private ImageView imageView; private ProgressDialog simpleWaitDialog; private final static String url = "http://i.imgur.com/pJv6ccq.jpg"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageView); new DownloadImage().execute(url); } private class DownloadImage extends AsyncTask{ @Override protected Bitmap doInBackground(String... param) { return downloadBitmap(param[0]); } @Override protected void onPreExecute() { simpleWaitDialog = ProgressDialog.show( Lewis.this, "Wait", "Downloading Image"); } protected void onPostExecute(Bitmap result) { imageView.setImageBitmap(result); simpleWaitDialog.dismiss(); } private Bitmap downloadBitmap(String url) { final DefaultHttpClient client = new DefaultHttpClient(); final HttpGet getRequest = new HttpGet(url); try { HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = null; try { inputStream = entity.getContent(); final Bitmap bitmap = BitmapFactory .decodeStream(inputStream); return bitmap; } finally { if (inputStream != null) { inputStream.close(); } entity.consumeContent(); } } } catch (Exception e) { getRequest.abort(); } return null; } } }
2014年11月29日 星期六
[Android_ColorPicker_Dialog][Android_調色盤_用dialog方式]
將以下程式加入貼到專案中。
import android.app.Dialog; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.SweepGradient; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; public class ColorPickerDialog extends Dialog { private final boolean debug = true; private final String TAG = "ColorPicker"; Context context; private String title; private int mInitialColor; private OnColorChangedListener mListener; public ColorPickerDialog(Context context, String title, OnColorChangedListener listener) { this(context, Color.BLACK, title, listener); } public ColorPickerDialog(Context context, int initialColor, String title, OnColorChangedListener listener) { super(context); this.context = context; mListener = listener; mInitialColor = initialColor; this.title = title; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowManager manager = getWindow().getWindowManager(); int height = (int) (manager.getDefaultDisplay().getHeight() * 0.5f); int width = (int) (manager.getDefaultDisplay().getWidth() * 0.7f); ColorPickerView myView = new ColorPickerView(context, height, width); setContentView(myView); setTitle(title); } private class ColorPickerView extends View { private Paint mPaint; private Paint mCenterPaint; private Paint mLinePaint; private Paint mRectPaint; private Shader rectShader; private float rectLeft; private float rectTop; private float rectRight; private float rectBottom; private final int[] mCircleColors; private final int[] mRectColors; private int mHeight; private int mWidth; private float r; private float centerRadius; private boolean downInCircle = true; private boolean downInRect; private boolean highlightCenter; private boolean highlightCenterLittle; public ColorPickerView(Context context, int height, int width) { super(context); this.mHeight = height - 36; this.mWidth = width; setMinimumHeight(height - 36); setMinimumWidth(width); mCircleColors = new int[] {0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,0xFFFFFF00, 0xFFFF0000}; Shader s = new SweepGradient(0, 0, mCircleColors, null); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setShader(s); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(50); r = width / 2 * 0.7f - mPaint.getStrokeWidth() * 0.5f; mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCenterPaint.setColor(mInitialColor); mCenterPaint.setStrokeWidth(5); centerRadius = (r - mPaint.getStrokeWidth() / 2 ) * 0.7f; mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mLinePaint.setColor(Color.parseColor("#72A1D1")); mLinePaint.setStrokeWidth(4); mRectColors = new int[]{0xFF000000, mCenterPaint.getColor(), 0xFFFFFFFF}; mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mRectPaint.setStrokeWidth(5); rectLeft = -r - mPaint.getStrokeWidth() * 0.5f; rectTop = r + mPaint.getStrokeWidth() * 0.5f + mLinePaint.getStrokeMiter() * 0.5f + 15; rectRight = r + mPaint.getStrokeWidth() * 0.5f; rectBottom = rectTop + 50; } @Override protected void onDraw(Canvas canvas) { canvas.translate(mWidth / 2, mHeight / 2 - 50); canvas.drawCircle(0, 0, centerRadius, mCenterPaint); if (highlightCenter || highlightCenterLittle) { int c = mCenterPaint.getColor(); mCenterPaint.setStyle(Paint.Style.STROKE); if(highlightCenter) { mCenterPaint.setAlpha(0xFF); }else if(highlightCenterLittle) { mCenterPaint.setAlpha(0x90); } canvas.drawCircle(0, 0, centerRadius + mCenterPaint.getStrokeWidth(), mCenterPaint); mCenterPaint.setStyle(Paint.Style.FILL); mCenterPaint.setColor(c); } canvas.drawOval(new RectF(-r, -r, r, r), mPaint); if(downInCircle) { mRectColors[1] = mCenterPaint.getColor(); } rectShader = new LinearGradient(rectLeft, 0, rectRight, 0, mRectColors, null, Shader.TileMode.MIRROR); mRectPaint.setShader(rectShader); canvas.drawRect(rectLeft, rectTop, rectRight, rectBottom, mRectPaint); float offset = mLinePaint.getStrokeWidth() / 2; canvas.drawLine(rectLeft - offset, rectTop - offset * 2, rectLeft - offset, rectBottom + offset * 2, mLinePaint); canvas.drawLine(rectLeft - offset * 2, rectTop - offset, rectRight + offset * 2, rectTop - offset, mLinePaint); canvas.drawLine(rectRight + offset, rectTop - offset * 2, rectRight + offset, rectBottom + offset * 2, mLinePaint); canvas.drawLine(rectLeft - offset * 2, rectBottom + offset, rectRight + offset * 2, rectBottom + offset, mLinePaint); super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX() - mWidth / 2; float y = event.getY() - mHeight / 2 + 50; boolean inCircle = inColorCircle(x, y, r + mPaint.getStrokeWidth() / 2, r - mPaint.getStrokeWidth() / 2); boolean inCenter = inCenter(x, y, centerRadius); boolean inRect = inRect(x, y); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downInCircle = inCircle; downInRect = inRect; highlightCenter = inCenter; case MotionEvent.ACTION_MOVE: if(downInCircle && inCircle) { float angle = (float) Math.atan2(y, x); float unit = (float) (angle / (2 * Math.PI)); if (unit < 0) { unit += 1; } mCenterPaint.setColor(interpCircleColor(mCircleColors, unit)); if(debug) Log.v(TAG, ": " + x + "," + y); }else if(downInRect && inRect) { mCenterPaint.setColor(interpRectColor(mRectColors, x)); } if(debug) Log.v(TAG, "[MOVE] : " + highlightCenter + ": " + highlightCenterLittle + " : " + inCenter); if((highlightCenter && inCenter) || (highlightCenterLittle && inCenter)) { highlightCenter = true; highlightCenterLittle = false; } else if(highlightCenter || highlightCenterLittle) { highlightCenter = false; highlightCenterLittle = true; } else { highlightCenter = false; highlightCenterLittle = false; } invalidate(); break; case MotionEvent.ACTION_UP: if(highlightCenter && inCenter) { if(mListener != null) { mListener.colorChanged(mCenterPaint.getColor()); ColorPickerDialog.this.dismiss(); } } if(downInCircle) { downInCircle = false; } if(downInRect) { downInRect = false; } if(highlightCenter) { highlightCenter = false; } if(highlightCenterLittle) { highlightCenterLittle = false; } invalidate(); break; } return true; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(mWidth, mHeight); } private boolean inColorCircle(float x, float y, float outRadius, float inRadius) { double outCircle = Math.PI * outRadius * outRadius; double inCircle = Math.PI * inRadius * inRadius; double fingerCircle = Math.PI * (x * x + y * y); if(fingerCircle < outCircle && fingerCircle > inCircle) { return true; }else { return false; } } private boolean inCenter(float x, float y, float centerRadius) { double centerCircle = Math.PI * centerRadius * centerRadius; double fingerCircle = Math.PI * (x * x + y * y); if(fingerCircle < centerCircle) { return true; }else { return false; } } private boolean inRect(float x, float y) { if( x <= rectRight && x >=rectLeft && y <= rectBottom && y >=rectTop) { return true; } else { return false; } } private int interpCircleColor(int colors[], float unit) { if (unit <= 0) { return colors[0]; } if (unit >= 1) { return colors[colors.length - 1]; } float p = unit * (colors.length - 1); int i = (int)p; p -= i; // now p is just the fractional part [0...1) and i is the index int c0 = colors[i]; int c1 = colors[i+1]; int a = ave(Color.alpha(c0), Color.alpha(c1), p); int r = ave(Color.red(c0), Color.red(c1), p); int g = ave(Color.green(c0), Color.green(c1), p); int b = ave(Color.blue(c0), Color.blue(c1), p); // Log.e("colort2222", Integer.toString(Color.argb(a, r, g, b))); return Color.argb(a, r, g, b); } private int interpRectColor(int colors[], float x) { int a, r, g, b, c0, c1; float p; if (x < 0) { c0 = colors[0]; c1 = colors[1]; p = (x + rectRight) / rectRight; } else { c0 = colors[1]; c1 = colors[2]; p = x / rectRight; } a = ave(Color.alpha(c0), Color.alpha(c1), p); r = ave(Color.red(c0), Color.red(c1), p); g = ave(Color.green(c0), Color.green(c1), p); b = ave(Color.blue(c0), Color.blue(c1), p); return Color.argb(a, r, g, b); } private int ave(int s, int d, float p) { return s + Math.round(p * (d - s)); } } public interface OnColorChangedListener { void colorChanged(int color); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getmInitialColor() { return mInitialColor; } public void setmInitialColor(int mInitialColor) { this.mInitialColor = mInitialColor; } public OnColorChangedListener getmListener() { return mListener; } public void setmListener(OnColorChangedListener mListener) { this.mListener = mListener; } }
用法:在你要改顏色地方加入以下程式碼。
AlertDialog.Builder editDialog = new AlertDialog.Builder(DragListActivity.this); editDialog.setTitle("pick color"); dialog = new ColorPickerDialog(mContext, tempColor, "11", new ColorPickerDialog.OnColorChangedListener() { public void colorChanged(int color2) { //set your color variable } }); dialog.show();
[Android Animation _use extends Animation_3DFlip ][Android 讓layout有動畫_用extends Animation的方式_3D翻頁效果 ]
這篇用extends Animation 來做特效。
先將以下程式加入專案。
使用方法:
如果你是要點擊按鈕或item以後轉跳畫面,想要等動畫跑完再轉跳畫面,可以用以下方法,
600代表0.6秒後再轉跳畫面
先將以下程式加入專案。
public class Flip3dAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private Camera mCamera; public Flip3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = centerX; mCenterY = centerY; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); camera.save(); camera.rotateY(degrees); camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
使用方法:
如果你是要點擊按鈕或item以後轉跳畫面,想要等動畫跑完再轉跳畫面,可以用以下方法,
600代表0.6秒後再轉跳畫面
final float centerX = view.getWidth() / 2.0f; final float centerY = view.getHeight() / 2.0f; final Flip3dAnimation rotation = new Flip3dAnimation(0, 90, centerX, centerY); rotation.setDuration(500); rotation.setFillAfter(true); rotation.setInterpolator(new AccelerateInterpolator()); view.startAnimation(rotation); view.postDelayed(new Runnable() { public void run() { //do things here } },600);
[Android Animation _use xml][Android 讓layout有動畫_用xml的方式 ]
將動畫的xml檔寫如下的資料夾中
範例:
裡面的參數請自行微調。
最後副上一個也還不錯的animation lib
https://github.com/dkmeteor/ActivityAnimationLib
最近又有更新:http://developer.android.com/training/animation/cardflip.html
範例:
這邊用兩段式動畫當例子。 animSlide_up = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up); layout_login.setAnimation(animSlide_up); layout_login.setVisibility(View.GONE); animSlide_down = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down); layout_after_login.setAnimation(animSlide_down); layout_after_login.setVisibility(View.VISIBLE);
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" > < alpha android:duration = "1000" android:fromAlpha = "0.0" android:interpolator = "@android:anim/accelerate_interpolator" android:toAlpha = "1.0" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" > < alpha android:duration = "1000" android:fromAlpha = "1.0" android:interpolator = "@android:anim/accelerate_interpolator" android:toAlpha = "0.0" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> < alpha android:fromAlpha = "0.0" android:toAlpha = "1.0" android:interpolator = "@android:anim/accelerate_interpolator" android:duration = "600" android:repeatMode = "reverse" android:repeatCount = "infinite" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" > < scale android:duration = "1000" android:fromXScale = "1" android:fromYScale = "1" android:pivotX = "50%" android:pivotY = "50%" android:toXScale = "3" android:toYScale = "3" > </ scale > </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" > < scale android:duration = "1000" android:fromXScale = "1.0" android:fromYScale = "1.0" android:pivotX = "50%" android:pivotY = "50%" android:toXScale = "0.5" android:toYScale = "0.5" > </ scale > </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> < rotate android:fromDegrees = "0" android:toDegrees = "360" android:pivotX = "50%" android:pivotY = "50%" android:duration = "600" android:repeatMode = "restart" android:repeatCount = "infinite" android:interpolator = "@android:anim/cycle_interpolator" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> < set android:interpolator = "@android:anim/linear_interpolator" android:fillAfter = "true" > < translate android:fromXDelta = "0%p" android:toXDelta = "75%p" android:duration = "800" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" > < scale android:duration = "500" android:fromXScale = "1.0" android:fromYScale = "1.0" android:interpolator = "@android:anim/linear_interpolator" android:toXScale = "1.0" android:toYScale = "0.0" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" > < scale android:duration = "500" android:fromXScale = "1.0" android:fromYScale = "0.0" android:interpolator = "@android:anim/linear_interpolator" android:toXScale = "1.0" android:toYScale = "1.0" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" android:interpolator = "@android:anim/bounce_interpolator" > < scale android:duration = "500" android:fromXScale = "1.0" android:fromYScale = "0.0" android:toXScale = "1.0" android:toYScale = "1.0" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" android:interpolator = "@android:anim/linear_interpolator" > <!-- Use startOffset to give delay between animations --> <!-- Move --> < translate android:duration = "800" android:fillAfter = "true" android:fromXDelta = "0%p" android:startOffset = "300" android:toXDelta = "75%p" /> < translate android:duration = "800" android:fillAfter = "true" android:fromYDelta = "0%p" android:startOffset = "1100" android:toYDelta = "70%p" /> < translate android:duration = "800" android:fillAfter = "true" android:fromXDelta = "0%p" android:startOffset = "1900" android:toXDelta = "-75%p" /> < translate android:duration = "800" android:fillAfter = "true" android:fromYDelta = "0%p" android:startOffset = "2700" android:toYDelta = "-70%p" /> <!-- Rotate 360 degrees --> < rotate android:duration = "1000" android:fromDegrees = "0" android:interpolator = "@android:anim/cycle_interpolator" android:pivotX = "50%" android:pivotY = "50%" android:startOffset = "3800" android:repeatCount = "infinite" android:repeatMode = "restart" android:toDegrees = "360" /> </ set > |
<? xml version = "1.0" encoding = "utf-8" ?> android:fillAfter = "true" android:interpolator = "@android:anim/linear_interpolator" > < scale android:duration = "4000" android:fromXScale = "1" android:fromYScale = "1" android:pivotX = "50%" android:pivotY = "50%" android:toXScale = "4" android:toYScale = "4" > </ scale > <!-- Rotate 180 degrees --> < rotate android:duration = "500" android:fromDegrees = "0" android:pivotX = "50%" android:pivotY = "50%" android:repeatCount = "infinite" android:repeatMode = "restart" android:toDegrees = "360" /> </ set > |
裡面的參數請自行微調。
最後副上一個也還不錯的animation lib
https://github.com/dkmeteor/ActivityAnimationLib
最近又有更新:http://developer.android.com/training/animation/cardflip.html
訂閱:
文章 (Atom)