2016-03-07 63 views
-1

我試圖在活動中創建一個片段,其中包含一個自定義圖像視圖和視圖序列應該是按鈕,片段,進度條,但現在我得到按鈕後的進度條,沒有什麼地方應該是片段。我已經發布了片段和customView的活動,片段和java代碼的佈局。如果我使用setContent()方法在Activity的onCreate方法中使用CustomView設置視圖,那麼我只需獲取imageview,以便自定義imageview起作用。請幫助。集成自定義ImageView與片段

public class IFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.article_view, container, false); 
    } 
} 

CustomView.java

public class CustomView extends ImageView 
{ 
    private Canvas pcanvas; 
    private Bitmap bitmap; 
    Paint mPaint; 
    private int x = 0; 
    private int y = 0; 
    private static final String TAG="adsa"; 
    private int r=0; 


    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     Bitmap bmrot=BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); 

     mPaint = new Paint(); 

     mPaint.setAlpha(0); 


     mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
     mPaint.setAntiAlias(true); 

     Paint alphaPaint = new Paint(); 
     alphaPaint.setAlpha(128); 
     pcanvas = new Canvas(); 
     Log.e(TAG,"The bitmap width is "+bmrot.getWidth() +" and the height is "+bmrot.getHeight()); 
     bitmap = bmrot.createBitmap(bmrot.getWidth(), bmrot.getHeight(), Bitmap.Config.ARGB_8888); 

     setBackgroundColor(Color.parseColor("#4d4d4d")); 
     pcanvas.setBitmap(bitmap); 
     pcanvas.drawBitmap(bmrot, 0, 0, alphaPaint); 
    } 
    @Override 
    protected void onDraw(Canvas canvas) { 

     pcanvas.drawCircle(x, y, r, mPaint); 


     canvas.drawBitmap(bitmap, 0, 0, null); 

     super.onDraw(canvas); 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 


     x = (int) event.getX(); 
     y = (int) event.getY(); 
     r = blurSeek.getProgress(); 


     invalidate(); 
     return true; 
    } 
} 

article_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
    <com.example.android.proj.CustomView 
     android:id="@+id/signature_canvas" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

活動xml文件

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/dsa" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.android.imagegallery.UploadImageActivity" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:id="@+id/thedd" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <Button android:text="Upload" 
       android:id="@+id/Btn1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
     </Button> 




    </LinearLayout> 

    <fragment android:name="com.example.android.imagegallery.ImageFragment" 
       android:id="@+id/article_fragment" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 


    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 

     /> 

</LinearLayout> 

顯示在logcat的(日誌級別 - ERROR) -

03-07 15:57:32.597 5996-5996/com.example.android.imagegallery E/SELinux: [DEBUG] seapp_context_lookup: seinfoCategory = default 
03-07 15:57:32.597 5996-5996/com.example.android.imagegallery E/dalvikvm: >>>>> Normal User 
03-07 15:57:32.597 5996-5996/com.example.android.imagegallery E/dalvikvm: >>>>> com.example.android.imagegallery [ userId:0 | appId:10035 ] 
03-07 15:57:32.607 5996-5996/com.example.android.imagegallery E/SELinux: [DEBUG] seapp_context_lookup: seinfoCategory = default 
03-07 15:57:33.407 5996-5996/com.example.android.imagegallery E/adsa: The bitmap width is 540 and the height is 374 
+0

任何錯誤在問題中添加日誌 –

+0

@Dhawal Sodha Parmar在logcat中沒有錯誤 – zek54

回答

0

您應該添加默認構造自定義ImageView的

public CustomView(Context context) { 
    super(context); 
} 

public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    Bitmap bmrot=BitmapFactory.decodeResource(context.getResources(), R.drawable.icon); 

    mPaint = new Paint(); 

    mPaint.setAlpha(0); 


    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    mPaint.setAntiAlias(true); 

    Paint alphaPaint = new Paint(); 
    alphaPaint.setAlpha(128); 
    pcanvas = new Canvas(); 
    Log.e(TAG,"The bitmap width is "+bmrot.getWidth() +" and the height is "+bmrot.getHeight()); 
    bitmap = bmrot.createBitmap(bmrot.getWidth(), bmrot.getHeight(), Bitmap.Config.ARGB_8888); 

    setBackgroundColor(Color.parseColor("#4d4d4d")); 
    pcanvas.setBitmap(bitmap); 
    pcanvas.drawBitmap(bmrot, 0, 0, alphaPaint); 
} 

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
} 

希望這會幫助你。

+0

對不起,但它不能解決問題。 – zek54

+0

@ zek54,分享你的崩潰日誌。 –

+0

我已經分享了錯誤日誌。 – zek54