
这是代码
我的自定义相对层面
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://schemas.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>解决方法 一些我如何通过在LayoutZoomingActivity中添加以下代码来解决此问题.
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 – 自定义相对布局缩放?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)