将自定义ImageView与片段集成

时间:2016-03-07 10:08:45

标签: android android-fragments android-custom-view

我正在尝试在活动中创建一个包含自定义图像视图的片段,视图序列应该是按钮,片段,进度条但是现在我在按钮后面得到了进度条,没有任何片段应该是我已经发布了片段和customView的活动,片段和java代码的布局。如果我使用setContent()方法在onCreate活动方法中使用CustomView设置视图,我只需获取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中显示(日志级别 - 错误) -

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

1 个答案:

答案 0 :(得分: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);
 }

希望这会对你有所帮助。

相关问题