2014-02-12 71 views
4

我一直在努力的應用程序類似於Pic Frame,Insta collage一些如何成功地執行此任務,並創建代碼來使這樣的框架我已經使用了多點觸摸這是在一個框架中創建多個部分,並使每個部分工作singleton

MultiTouchview類

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 


/** 
* Canvas View for the MultiTouch controller 
* @author 2dwarfs.com 
* 
*/ 

public class MultiTouchView extends View implements MultiTouchObjectCanvas<PinchWidget> { 

    private static final int UI_MODE_ROTATE = 1; 
    private static final int UI_MODE_ANISOTROPIC_SCALE = 2; 
    private int mUIMode = UI_MODE_ROTATE; 
    ArrayList<PinchWidget> alist = new ArrayList<PinchWidget>(); 

    private MultiTouchController<PinchWidget> mMultiTouchController = new MultiTouchController<PinchWidget>(this); 

    private int mWidth, mHeight; 

    private PinchWidget mPinchWidget; 
    private Context mContext; 

    public MultiTouchView(Context context) { 
     super(context); 
     //mContext = context; 
    } 

    public void clearCache(){ 
     alist.clear(); 
    } 

    public MultiTouchView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
    } 

    public MultiTouchView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void setPinchWidget(Bitmap bitmap) { 
     mPinchWidget = new PinchWidget(bitmap); 
     alist.add(mPinchWidget); 
     mPinchWidget.init(mContext.getResources()); 
     invalidate(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     mWidth = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec); 
     mHeight = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec); 
     setMeasuredDimension(mWidth, mHeight); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawColor(Color.TRANSPARENT); 
     for(PinchWidget mmPinchWidget : alist) 
      mmPinchWidget.draw(canvas); 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     return mMultiTouchController.onTouchEvent(ev); 
    } 

    @Override 
    public PinchWidget getDraggableObjectAtPoint(PointInfo pt) { 
     float x = pt.getX(), y = pt.getY(); 
     for(PinchWidget mPinchWidget : alist) { 
     if (mPinchWidget.containsPoint(x, y)) { 
      return mPinchWidget; 
     } 
     } 

     return null; 
    } 

    @Override 
    public void getPositionAndScale(PinchWidget pinchWidget, PositionAndScale objPosAndScaleOut) { 
     objPosAndScaleOut.set(pinchWidget.getCenterX(), pinchWidget.getCenterY(), 
       (mUIMode & UI_MODE_ANISOTROPIC_SCALE) == 0, 
       (pinchWidget.getScaleFactor() + pinchWidget.getScaleFactor())/2, 
       (mUIMode & UI_MODE_ANISOTROPIC_SCALE) != 0, 
       pinchWidget.getScaleFactor(), 
       pinchWidget.getScaleFactor(), 
       (mUIMode & UI_MODE_ROTATE) != 0, 
       pinchWidget.getAngle()); 
    } 

    @Override 
    public boolean setPositionAndScale(PinchWidget pinchWidget, PositionAndScale newImgPosAndScale, PointInfo touchPoint) { 
     boolean ok = pinchWidget.setPos(newImgPosAndScale, mUIMode, UI_MODE_ANISOTROPIC_SCALE, touchPoint.isMultiTouch()); 
     if(ok) { 
      invalidate(); 
     } 

     return ok; 
    } 

    @Override 
    public void selectObject(PinchWidget pinchWidget, PointInfo touchPoint) { 
     if(touchPoint.isDown()) { 
      mPinchWidget = pinchWidget; 
     } 

     invalidate(); 
    } 
} 

並用視圖類此幀像

Two.xml XML文件的兩幀

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/transparent" 
    android:orientation="horizontal" > 

    <com.example.imageframe.MultiTouchView 
     android:orientation="horizontal" 
     android:id="@+id/firstone" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="5.0dip" 
     android:layout_marginLeft="5.0dip" 
     android:layout_marginRight="5.0dip" 
     android:layout_marginTop="5.0dip" 
     android:layout_weight="0.5" 
     android:background="@drawable/back" /> 

    <com.example.imageframe.MultiTouchView 
     android:orientation="horizontal" 
     android:id="@+id/secondone" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="5.0dip" 
     android:layout_marginRight="5.0dip" 
     android:layout_marginTop="5.0dip" 
     android:layout_weight="0.5" 
     android:background="@drawable/back" /> 

</LinearLayout> 

雖然編碼,我認爲它是一個非常複雜的方式來創建和使用這樣的幀。此外,這些框架特別是正方形/長方形,我無法動態地製作彎曲的邊框或改變框架形狀(使框架分區大/小或改變框架的形狀)。

我已經搜索了每一件事,找到一個合適的解決方案來實現這個解決方案,但沒有成功。

請儘快指導我動態地創建/操作/編輯框架,或者提供一些代碼片段來實現這種功能。

+0

您可以設置所需視圖的可見性,以在需要時「消失」 – user2609847

回答

0

你可以使用片段。分別設計你的佈局。例如fragmentNumberOne.xml和fragmentNumberTwo.xml

在您的xml主要活動中放置一個FrameLayout;

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

在您的活動;

Fragment fragment = new YourFragment(); 
Bundle args = new Bundle(); 
args.putInt("fragmentLayout", R.layout.fragmentNumberOne); 
fragment.setArguments(args); 

FragmentManager fragmentManager = getSupportFragmentManager(); 
fragmentManager.beginTransaction() 
     .replace(R.id.content_frame, fragment).commit(); 

並創建一個類;

public class YourFragment extends Fragment { 

public YourFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    int fragmentLayout = getArguments().getInt("fragmentLayout"); 
    final View rootView = inflater 
      .inflate(fragmentLayout, container, false); 

    if (fragmentLayout == R.layout.fragmentNumberOne) { 
     ... 
     write your code here 
     ... 
    } else if (fragmentLayout == R.layout.fragmentNumberTwo) { 
     ... 
     write your code here 
     ... 
    } 

    return rootView; 
} 
相關問題