In the development process, in order to achieve a certain effect and specific functions that we did not know, we turned to excellent open-source projects on the Internet and found that the open-source Library Project (Library Project) was referenced during use ), A lot of problems can be solved, and a new idea is also provided. A good software company also needs to have its own Library Project (Library Project), so that repeated development is unnecessary, this is just your own idea. The following is a record of your experiences and experiences. The following is your writing style.
Application Scenario: during project development, public libraries within the company or excellent open source project libraries are often used to achieve rapid development and code reuse. To integrate other projects in the developed project, because the integration project is huge and you do not want to copy the project mechanically, you want to make the integrated project into a project library. The reference of the Library Project (Library Project) becomes an urgent need based on this requirement. It will be convenient and convenient to upgrade and expand the integration Project in the future. Therefore, we have to understand how to create and reference an Android Library Project. [For more information, see http://blog.csdn.net/mahoking/article/details/36405219]
Usage: Step 1: Create a project named MyLib and click Next ].
Step 2: deselect the red check box. If you do not select [Create M launcher icon] or [Create activity], check [Make this project as a library].
Step 3: first create a package name and add the MyImageView. java file. [MyImageView. java references others' code. Thank you for choosing this document .]
Package com. example. lib; import android. content. context; import android. graphics. matrix; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android. OS. handler; import android. util. attributeSet; import android. view. motionEvent; import android. widget. imageView; public class MyImageView extends ImageView {private static final String TAG = MyImageView; private stat Ic final int scale_performance_init = 0; private static final int SCALING = 1; private static final int SCALE_ADD_INIT = 6;/*** control width */private int mWidth; /*** control height */private int mHeight;/*** control width: 1/2 */private int mCenterWidth; /*** the height of the control is 1/2 */private int mCenterHeight;/*** set a scaling constant */private float mMinScale = 0.85f; /*** end of scaling? */private boolean isFinish = true; public MyImageView (Context context) {this (context, Null);} public MyImageView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public MyImageView (Context context Context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}/*** required initialization */@ Overrideprotected void onLayout (boolean changed, int left, int top, int right, int bottom) {super. onLayout (changed, left, top, right, bottom); if (changed) {mWidth = getWidth ()-getPaddingLe Ft ()-getPaddingRight (); mHeight = getHeight ()-getPaddingTop ()-getPaddingBottom (); mCenterWidth = mWidth/2; mCenterHeight = mHeight/2; drawable drawable = getDrawable (); BitmapDrawable bd = (BitmapDrawable) drawable; bd. setAntiAlias (true) ;}@overridepublic boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: float X = event. getX (); float Y = event. get Y (); mScaleHandler. sendEmptyMessage (scale_performance_init); break; case MotionEvent. ACTION_UP: mScaleHandler. sendEmptyMessage (SCALE_ADD_INIT); break;} return true;}/*** Handler controlling scaling */private Handler mScaleHandler = new Handler () {private Matrix matrix = new Matrix (); private int count = 0; private float s;/*** whether the click event has been called */private boolean isClicked; public void handleMessage (android. OS. message msg) {matrix. Set (getImageMatrix (); switch (msg. what) {case scale_performance_init: if (! IsFinish) {mScaleHandler. sendEmptyMessage (scale_performance_init);} else {isFinish = false; count = 0; s = (float) Math. sqrt (Math. sqrt (mMinScale); beginScale (matrix, s); mScaleHandler. sendEmptyMessage (SCALING);} break; case SCALING: beginScale (matrix, s); if (count <4) {mScaleHandler. sendEmptyMessage (SCALING);} else {isFinish = true; if (MyImageView. this. mOnViewClickListener! = Null &&! IsClicked) {isClicked = true; MyImageView. this. mOnViewClickListener. onViewClick (MyImageView. this);} else {isClicked = false;} count ++; break; case SCALE_ADD_INIT: if (! IsFinish) {mScaleHandler. sendEmptyMessage (SCALE_ADD_INIT);} else {isFinish = false; count = 0; s = (float) Math. sqrt (Math. sqrt (1.0f/mMinScale); beginScale (matrix, s); mScaleHandler. sendEmptyMessage (SCALING) ;}break ;}}; protected void sleep (int I) {try {Thread. sleep (I);} catch (InterruptedException e) {e. printStackTrace () ;}/ ***** scale ** @ param matrix * @ param scale */private synchronized void beginScale (Matrix matrix, float scale) {matrix. postScale (scale, scale, mCenterWidth, mCenterHeight); setImageMatrix (matrix);}/*** callback interface */private OnViewClickListener mOnViewClickListener; public void setOnClickIntent (OnViewClickListener onViewClickListener) {this. mOnViewClickListener = onViewClickListener;} public interface OnViewClickListener {void onViewClick (MyImageView view );}}
Step 4: Create another project MyProj, check [Create M launcher icon] and [Create activity], and do not check [Make this project as a library].
Step 5: reference the MyLib project.
Step 6: Write activtiy_main.xml and the corresponding Activity.
[Activtiy_main.xml]
[MainActivtiy. java]
Package com. example. myproj; import com. example. lib. myImageView; import com. example. lib. myImageView. onViewClickListener; import android. OS. bundle; import android. app. activity; import android. widget. toast; public class MainActivity extends Activity {private MyImageView imageView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity _ Main); initViews ();} private void initViews () {imageView = (MyImageView) findViewById (R. id. myImageView); imageView. setOnClickIntent (new OnViewClickListener () {@ Overridepublic void onViewClick (MyImageView view) {Toast. makeText (getApplicationContext (), you clicked on this cute expression !, Toast. LENGTH_SHORT). show ();}});}}
Step 7: run the project MyProj and the following results are displayed.
======= Click to download ======= [for Reprinted use, please specify the Source: http://blog.csdn.net/mahoking/article/details/36405219]
References 1. http://blog.sina.com.cn/s/blog_783ede0301016ip2.html