大佬教程收集整理的这篇文章主要介绍了android – 自定义相对布局缩放?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是代码
我的自定义相对层面
public class scaleLayout extends RelativeLayout { private float mScaleFactor=1.0f; private long lastTouchTime = -1; public scaleLayout(Context context) { super(context); // setWillNotDraw(false); } public scaleLayout(Context context,AttributeSet attrs) { super(context,attrs); //setWillNotDraw(false); // TODO Auto-generated constructor stub } /* @Override public Boolean dispatchTouchEvent(MotionEvent event) { return super.dispatchTouchEvent(event); } */ @Override public Boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); } @Override public Boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { long thisTime = System.currentTimeMillis(); if (thisTime - lastTouchTime < 250) { // Double tap mScaleFactor=1.5f; invalidate(); lastTouchTime = -1; } else { // Too slow :) /* mScaleFactor=1.0f; invalidate();*/ lastTouchTime = thisTime; } } return super.onInterceptTouchEvent(ev); } @Override protected void dispatchDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.scale(mScaleFactor,mScaleFactor); super.dispatchDraw(canvas); canvas.restore(); } @Override public ViewParent invalidateChildInParent(int[] LOCATIOn,Rect dirty) { // TODO Auto-generated method stub return super.invalidateChildInParent(LOCATIOn,dirty); } protected void onLayout(Boolean changed,int l,int t,int r,int b) { int count = getChildCount(); for(int i=0;i<count;i++){ View child = getChildAt(i); if(child.getVisibility()!=GONE){ RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams(); child.layout( (int)(params.leftMargin * mScaleFactor),(int)(params.topMargin * mScaleFactor),(int)((params.leftMargin + child.getMeasuredWidth()) * mScaleFactor),(int)((params.topMargin + child.getMeasuredHeight()) * mScaleFactor) ); } } }
这是活动
public class LayoutZoomingActivity extends Activity implements OnTouchListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.main); ImageView img1 = (ImageView) findViewById(R.id.imageView1); img1.setOnTouchListener(this); ImageView img2 = (ImageView) findViewById(R.id.imageView2); img2.setOnTouchListener(this); } @Override public Boolean onTouch(View v,MotionEvent event) { // TODO Auto-generated method stub ImageView iv= (ImageView) v; v.setVisibility(View.INVISIBLE); return false; }
这是xml
<com.layoutzooming.scaleLayout xmlns:android="http://scheR_677_11845@as.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:BACkground="@drawable/gm01" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="jhkibnkij" android:layout_centerInParent="true" android:textColor="#FFFFFF" android:textSize="25dp" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layouT_Below="@+id/textView1" android:layout_marginLeft="500dp" android:layout_marginTop="250dp" android:BACkground="#000" android:src="@drawable/dih01" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layouT_Below="@+id/textView1" android:layout_marginLeft="350dp" android:layout_marginTop="250dp" android:BACkground="#000" android:src="@drawable/dih02" /> </RelativeLayout> </com.layoutzooming.scaleLayout>
public class LayoutZoomingActivity extends Activity implements OnTouchListener { ImageView img1; ImageView img2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.main); img1 = (ImageView) findViewById(R.id.imageView1); img1.setOnTouchListener(this); img2 = (ImageView) findViewById(R.id.imageView2); img2.setOnTouchListener(this); } @Override public Boolean onTouch(View v,MotionEvent event) { // TODO Auto-generated method stub Log.d("b","b"); if(isViewContains(img1,(int)event.getX(),(int)event.getY())) { img1.setVisibility(View.GONE); } if(isViewContains(img2,(int)event.getY())) { img2.setVisibility(View.GONE); } return false; } @Override public Boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub Log.d("onTouchEvent c","c"+(int)event.getX()+","+(int)event.getY()); if(isViewContains(img1,(int)event.getY())) { img1.setVisibility(View.GONE); img1.invalidate(); } if(isViewContains(img2,(int)(event.getX()/scaleLayout.mScaleFactor),(int)(event.getY()/scaleLayout.mScaleFactor))) { img2.setVisibility(View.GONE); img2.invalidate(); } return super.onTouchEvent(event); } private Boolean isViewContains(View view,int rx,int ry) { int[] l = new int[2]; view.getLOCATIOnOnScreen(l); int x = l[0]; int y = l[1]; int w = view.getWidth(); int h = view.getHeight(); Log.d("isViewContains::"+x+"::"+y+"::"+w+"::"+h,rx+"::"+ry); if (rx < x || rx > x + w || ry < y || ry > y + h) { return false; } return true; } }
您可以试试这个并根据您的要求定制.
以上是大佬教程为你收集整理的android – 自定义相对布局缩放?全部内容,希望文章能够帮你解决android – 自定义相对布局缩放?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。