集成自定义ImageView与片段

问题描述:

我试图在活动中创建一个片段,其中包含一个自定义图像视图和视图序列应该是按钮,片段,进度条,但现在我得到按钮后的进度条,没有什么地方应该是片段。我已经发布了片段和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

您应该添加默认构造自定义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